You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: chapters/en/slides/module6/module6_01.Rmd
+133Lines changed: 133 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -515,6 +515,139 @@ Although side effects are not recommended, there are cases where either we must
515
515
This leads to the next question of *How*? Good news - the answer is coming later on in this module!
516
516
517
517
518
+
---
519
+
520
+
## The deal with *print()*
521
+
522
+
```{python}
523
+
print('A regular string')
524
+
```
525
+
526
+
527
+
```{python}
528
+
a_number_variable = 54.346
529
+
530
+
print(a_number_variable)
531
+
```
532
+
533
+
534
+
Notes:
535
+
536
+
What is `print()`?
537
+
538
+
We have not talked about this function in large detail but we do know `print()` will print whatever variable or item you call in it. It can be an especially handy one when debugging.
539
+
540
+
We can use it to print some code directly or from a variable like we see here.
541
+
542
+
It's important that we address using the print statement vs using return in a function as they are quite different.
543
+
544
+
Let's see why.
545
+
546
+
---
547
+
548
+
```{python}
549
+
def squares_a_list(numerical_list):
550
+
new_squared_list = list()
551
+
for number in numerical_list:
552
+
new_squared_list.append(number ** 2)
553
+
return new_squared_list
554
+
```
555
+
556
+
557
+
<br/>
558
+
<br/>
559
+
560
+
```{python}
561
+
def squares_a_list_print(numerical_list):
562
+
new_squared_list = list()
563
+
for number in numerical_list:
564
+
new_squared_list.append(number ** 2)
565
+
print(new_squared_list)
566
+
```
567
+
568
+
569
+
570
+
571
+
572
+
Notes:
573
+
574
+
Here er have our `squares_a_list` function.
575
+
Let's create a new function called `squares_a_list_print` where instead of returning the new variable `new_squared_list`, we print it instead.
576
+
577
+
The only difference here is that in `squares_a_list` we return `new_squared_list` and in `squares_a_list_print` we are printing `new_squared_list`.
578
+
579
+
---
580
+
581
+
582
+
```{python}
583
+
numbers = [2, 3, 5]
584
+
```
585
+
586
+
```{python}
587
+
squares_a_list(numbers)
588
+
```
589
+
590
+
```{python}
591
+
squares_a_list_print(numbers)
592
+
```
593
+
594
+
Notes:
595
+
596
+
Let's see what happens when we call these functions now.
597
+
598
+
If we call them both without assigning them to an object, it looks like these functions do identical things.
599
+
600
+
Both output the new list.
601
+
602
+
---
603
+
604
+
605
+
```{python}
606
+
return_func_var = squares_a_list(numbers)
607
+
```
608
+
609
+
No output here since we are assigning to an object.
610
+
611
+
<br/>
612
+
613
+
```{python}
614
+
print_func_var = squares_a_list_print(numbers)
615
+
```
616
+
617
+
But we do have output here because the `print()` statement is executed within the function here.
618
+
619
+
<br/>
620
+
621
+
<br/>
622
+
623
+
624
+
```{python}
625
+
return_func_var
626
+
```
627
+
Here we have an output since the list was returned and saved to the variable.
628
+
629
+
```{python}
630
+
print_func_var
631
+
```
632
+
633
+
No output here since the output was only printed statement and not saved in the object.
634
+
635
+
Notes:
636
+
637
+
This time let's instead save them to objects.
638
+
639
+
When we call and save `squares_a_list(numbers)`, you'll see that nothing is printed or outputted, yet when we do the same thing with `squares_a_list_print(numbers)`, the new list is outputted.
640
+
641
+
This is somewhat expected since we are not printing anything in our `squares_a_list()` function but we are in `squares_a_list_print()`.
642
+
643
+
Now let's see what our variables return.
644
+
645
+
`return_func_var` actually returns the output from the function, but `print_func_var` doesn't!
646
+
647
+
That's because the `print()` function, when used in a function that you make, is a **side effect**!
648
+
649
+
In order for us to save the output of our functions to a variable, we must use return, otherwise we are only producing a side effect instead of returning an actual output.
0 commit comments