Skip to content

Commit 7a09901

Browse files
Include while loops and break and continue statements (#9)
* Include while loops and break and continue statements * Update slides/basics.qmd Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com> * Update slides/basics.qmd Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com> * Update slides/basics.qmd Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com> * Update slides/basics.qmd Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com> * Update slides/basics.qmd Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com> * Invert order, change example * Add `and` and `or` --------- Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>
1 parent fe631e9 commit 7a09901

File tree

1 file changed

+140
-26
lines changed

1 file changed

+140
-26
lines changed

slides/basics.qmd

Lines changed: 140 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,85 @@ print(my_dict['a_number'])
449449
:::
450450
:::::
451451

452-
## Loops {.smaller}
452+
## Conditional statements {.smaller}
453+
::::: {.columns}
454+
::: {.column .incremental width="55%"}
455+
* Execute code only if a condition is met
456+
* Use `if`, `elif` (else if), and `else`
457+
:::
458+
::: {.column width="45%"}
459+
:::: {.fragment .fade-in}
460+
```{python}
461+
#| echo: true
462+
#| output-location: fragment
463+
#| code-line-numbers: "|1|3|5|7,8|"
464+
465+
porridge_temp = 45
466+
467+
if porridge_temp < 40:
468+
print("Too cold!")
469+
elif porridge_temp > 50:
470+
print("Too hot!")
471+
else:
472+
print("Just right!")
473+
```
474+
::::
475+
:::
476+
:::::
477+
478+
## Question {.smaller}
479+
* Write a loop that goes through the numbers 0 to 10
480+
* For each number, print whether it is even or odd
481+
+ Hint: use the modulus operator `%` and `==` to check for evenness
482+
483+
::: {.fragment .fade-in}
484+
```{python}
485+
#| echo: true
486+
#| output-location: fragment
487+
#| code-line-numbers: "|1|2,3|4,5|"
488+
489+
for i in range(11):
490+
if i % 2 == 0:
491+
print(f"{i} is Even")
492+
else:
493+
print(f"{i} is Odd")
494+
```
495+
:::
496+
497+
## Using `and` and `or` {.smaller}
498+
::::: {.columns}
499+
::: {.column .incremental width="55%"}
500+
* `and` and `or` are logical operators
501+
* Used to combine conditions
502+
:::
503+
504+
::: {.column width="45%"}
505+
:::: {.fragment .fade-in}
506+
```{python}
507+
#| echo: true
508+
#| output-location: fragment
509+
print(True and False)
510+
print(True or False)
511+
```
512+
::::
513+
514+
:::: {.fragment .fade-in}
515+
```{python}
516+
#| echo: true
517+
#| output-location: fragment
518+
ammount_of_rain = 0
519+
ammount_of_uv_rays = 10
520+
521+
if ammount_of_rain > 10 and ammount_of_uv_rays > 0:
522+
print("Take umbrella and sunscreen!")
523+
else:
524+
print("Enjoy the sun!")
525+
```
526+
::::
527+
:::
528+
:::::
529+
530+
## For loops {.smaller}
453531
::::: {.columns}
454532
::: {.column .incremental width="55%"}
455533
* Do the same thing multiple times
@@ -484,7 +562,7 @@ for i in range(5):
484562
:::
485563
:::::
486564

487-
## Loops {.smaller}
565+
## For loops {.smaller}
488566
::::: {.columns}
489567
::: {.column .incremental width="55%"}
490568
* You can loop over any iterable
@@ -530,6 +608,41 @@ for index, animal in enumerate(my_list):
530608
:::
531609
:::::
532610

611+
## Break and continue statements {.smaller}
612+
::::: {.columns}
613+
::: {.column .incremental width="55%"}
614+
* `break` out of a loop
615+
* `continue` to the next iteration
616+
:::
617+
618+
::: {.column width="45%"}
619+
:::: {.fragment .fade-in}
620+
```{python}
621+
#| echo: true
622+
#| output-location: fragment
623+
for i in range(10):
624+
if i == 5:
625+
break
626+
print(i)
627+
```
628+
::::
629+
630+
:::: {.fragment .fade-in}
631+
```{python}
632+
#| echo: true
633+
#| output-location: fragment
634+
for i in range(10):
635+
if i == 5:
636+
continue
637+
print(i)
638+
```
639+
::::
640+
:::
641+
642+
:::::
643+
644+
645+
533646
## Question {.smaller}
534647
::: {.fragment .fade-in }
535648
* Create a list of integers from 0 to 5
@@ -568,48 +681,49 @@ print(sum_of_squares)
568681
```
569682
:::
570683

571-
## Conditional statements {.smaller}
684+
## While loops {.smaller}
572685
::::: {.columns}
573686
::: {.column .incremental width="55%"}
574-
* Execute code only if a condition is met
575-
* Use `if`, `elif` (else if), and `else`
687+
* What if you don't know when you should stop iterating?
688+
* Use `while`!
689+
* Does something while a condition is met
576690
:::
691+
577692
::: {.column width="45%"}
578693
:::: {.fragment .fade-in}
579694
```{python}
580695
#| echo: true
581696
#| output-location: fragment
582-
#| code-line-numbers: "|1|3|5|7,8|"
583-
584-
porridge_temp = 45
585-
586-
if porridge_temp < 40:
587-
print("Too cold!")
588-
elif porridge_temp > 50:
589-
print("Too hot!")
590-
else:
591-
print("Just right!")
697+
expected_result = 10
698+
result = 0
699+
while not expected_result == result:
700+
print("Not there yet...")
701+
print(f"Result is {result}")
702+
result += 1
703+
print("We got there!")
592704
```
593705
::::
594706
:::
707+
595708
:::::
596709

597-
## Question {.smaller}
598-
* Write a loop that goes through the numbers 0 to 10
599-
* For each number, print whether it is even or odd
600-
+ Hint: use the modulus operator `%` and `==` to check for evenness
601710

711+
## Question {.smaller}
712+
<!-- exercise with while loop and break statement -->
713+
::: {.fragment .fade-in }
714+
* Write a loop that only prints numbers divisible by 3 until you get to 30
715+
* But skip printing if the number is divisible by 5
716+
* Use a while loop
717+
:::
602718
::: {.fragment .fade-in}
603719
```{python}
604720
#| echo: true
605721
#| output-location: fragment
606-
#| code-line-numbers: "|1|2,3|4,5|"
607-
608-
for i in range(11):
609-
if i % 2 == 0:
610-
print(f"{i} is Even")
611-
else:
612-
print(f"{i} is Odd")
722+
number = 0
723+
while number < 30:
724+
if number % 3 == 0 and number % 5 != 0:
725+
print(number)
726+
number += 1
613727
```
614728
:::
615729

0 commit comments

Comments
 (0)