Skip to content

Commit 68f02a1

Browse files
committed
added print slides
1 parent 4452e4c commit 68f02a1

File tree

5 files changed

+315
-21
lines changed

5 files changed

+315
-21
lines changed

chapters/en/slides/module6/module6_01.Rmd

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,139 @@ Although side effects are not recommended, there are cases where either we must
515515
This leads to the next question of *How*? Good news - the answer is coming later on in this module!
516516

517517

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.
650+
518651
---
519652

520653

chapters/en/slides/module6/module6_01.md

Lines changed: 180 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ as we loop through the input list.
139139
But what happens if we try and print this variable outside of the
140140
function?
141141

142-
Yikes\! Where did `new_squared_list` go?
142+
Yikes! Where did `new_squared_list` go?
143143

144-
It doesn’t seem to exist\! That’s not entirely true.
144+
It doesn’t seem to exist! That’s not entirely true.
145145

146146
In Python, `new_squared_list` is something we call a ***local
147147
variable***.
@@ -214,7 +214,7 @@ recognized outside of any function but also recognized inside functions.
214214
Let’s take a look at what happens when we add `a_new_variable`, which is
215215
a global variable,e and refer to it in the `squares_a_list` function.
216216

217-
The function recognizes the global variable\!
217+
The function recognizes the global variable!
218218

219219
It’s important to note that, although functions recognize global
220220
variables, it’s not good practice to have functions reference objects
@@ -227,9 +227,7 @@ We will learn more about this later in the module.
227227
<br> <br> <br>
228228

229229
<center>
230-
231230
<img src='/module6/starbucks.png' width="100%" alt="404 image">
232-
233231
</center>
234232

235233
[Attribution - Starbucks](https://unsplash.com/photos/42ui88Qrxhw)
@@ -415,7 +413,7 @@ Let’s take a list that we define in our global environment called
415413
`global_list` and add `99` to the list in the local environment.
416414

417415
The list that we defined globally was able to be modified inside the
418-
function and have the changes reflected back in the global environment\!
416+
function and have the changes reflected back in the global environment!
419417

420418
What is going on?
421419

@@ -440,10 +438,10 @@ cereal.head()
440438
4 Almond Delight R Cold 110 2 2 200 1.0 14.0 8 1 25 3 1.0 0.75 34.384843
441439
```
442440

443-
- `.drop()`
444-
- `.assign()`
445-
- `.sort_values()`
446-
- `.rename()`
441+
- `.drop()`
442+
- `.assign()`
443+
- `.sort_values()`
444+
- `.rename()`
447445

448446
Notes:
449447

@@ -452,10 +450,10 @@ dataframe.
452450

453451
Since the beginning of this course, we have been using verbs such as;
454452

455-
- `.drop()`
456-
- `.assign()`
457-
- `.sort_values()`
458-
- `.rename()`
453+
- `.drop()`
454+
- `.assign()`
455+
- `.sort_values()`
456+
- `.rename()`
459457

460458
Where we modify a dataframe and save the modification as a new dataframe
461459
object.
@@ -599,7 +597,7 @@ environment they originated in.
599597

600598
## Side Effect Documentation
601599

602-
- If your functions have side-effects, they should be documented.
600+
- If your functions have side-effects, they should be documented.
603601

604602
Notes:
605603

@@ -608,11 +606,176 @@ we must have side-effects in our functions, or there is no way to avoid
608606
it. In these cases, it is extremely important that we document it.
609607

610608
This leads to the next question of *How*? Good news - the answer is
611-
coming later on in this module\!
609+
coming later on in this module!
610+
611+
---
612+
613+
## The deal with *print()*
614+
615+
``` python
616+
print('A regular string')
617+
```
618+
619+
```out
620+
A regular string
621+
```
622+
623+
``` python
624+
a_number_variable = 54.346
625+
626+
print(a_number_variable)
627+
```
628+
629+
```out
630+
54.346
631+
```
632+
633+
Notes:
634+
635+
What is `print()`?
636+
637+
We have not talked about this function in large detail but we do know
638+
`print()` will print whatever variable or item you call in it. It can be
639+
an especially handy one when debugging.
640+
641+
We can use it to print some code directly or from a variable like we see
642+
here.
643+
644+
It’s important that we address using the print statement vs using return
645+
in a function as they are quite different.
646+
647+
Let’s see why.
648+
649+
---
650+
651+
``` python
652+
def squares_a_list(numerical_list):
653+
new_squared_list = list()
654+
for number in numerical_list:
655+
new_squared_list.append(number ** 2)
656+
return new_squared_list
657+
```
658+
659+
<br/> <br/>
660+
661+
``` python
662+
def squares_a_list_print(numerical_list):
663+
new_squared_list = list()
664+
for number in numerical_list:
665+
new_squared_list.append(number ** 2)
666+
print(new_squared_list)
667+
```
668+
669+
Notes:
670+
671+
Here er have our `squares_a_list` function. Let’s create a new function
672+
called `squares_a_list_print` where instead of returning the new
673+
variable `new_squared_list`, we print it instead.
674+
675+
The only difference here is that in `squares_a_list` we return
676+
`new_squared_list` and in `squares_a_list_print` we are printing
677+
`new_squared_list`.
678+
679+
---
680+
681+
``` python
682+
numbers = [2, 3, 5]
683+
```
684+
685+
``` python
686+
squares_a_list(numbers)
687+
```
688+
689+
```out
690+
[4, 9, 25]
691+
```
692+
693+
``` python
694+
squares_a_list_print(numbers)
695+
```
696+
697+
```out
698+
[4, 9, 25]
699+
```
700+
701+
Notes:
702+
703+
Let’s see what happens when we call these functions now.
704+
705+
If we call them both without assigning them to an object, it looks like
706+
these functions do identical things.
707+
708+
Both output the new list.
709+
710+
---
711+
712+
``` python
713+
return_func_var = squares_a_list(numbers)
714+
```
715+
716+
No output here since we are assigning to an object.
717+
718+
<br/>
719+
720+
``` python
721+
print_func_var = squares_a_list_print(numbers)
722+
```
723+
724+
```out
725+
[4, 9, 25]
726+
```
727+
728+
But we do have output here because the `print()` statement is executed
729+
within the function here.
730+
731+
<br/>
732+
733+
<br/>
734+
735+
``` python
736+
return_func_var
737+
```
738+
739+
```out
740+
[4, 9, 25]
741+
```
742+
743+
Here we have an output since the list was returned and saved to the
744+
variable.
745+
746+
``` python
747+
print_func_var
748+
```
749+
750+
No output here since the output was only printed statement and not saved
751+
in the object.
752+
753+
Notes:
754+
755+
This time let’s instead save them to objects.
756+
757+
When we call and save `squares_a_list(numbers)`, you’ll see that nothing
758+
is printed or outputted, yet when we do the same thing with
759+
`squares_a_list_print(numbers)`, the new list is outputted.
760+
761+
This is somewhat expected since we are not printing anything in our
762+
`squares_a_list()` function but we are in `squares_a_list_print()`.
763+
764+
Now let’s see what our variables return.
765+
766+
`return_func_var` actually returns the output from the function, but
767+
`print_func_var` doesn’t!
768+
769+
That’s because the `print()` function, when used in a function that you
770+
make, is a **side effect**!
771+
772+
In order for us to save the output of our functions to a variable, we
773+
must use return, otherwise we are only producing a side effect instead
774+
of returning an actual output.
612775

613776
---
614777

615-
# Let’s apply what we learned\!
778+
# Let’s apply what we learned!
616779

617780
Notes:
618781

0 commit comments

Comments
 (0)