@@ -449,7 +449,85 @@ print(my_dict['a_number'])
449
449
:::
450
450
:::::
451
451
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}
453
531
::::: {.columns}
454
532
::: {.column .incremental width="55%"}
455
533
* Do the same thing multiple times
@@ -484,7 +562,7 @@ for i in range(5):
484
562
:::
485
563
:::::
486
564
487
- ## Loops {.smaller}
565
+ ## For loops {.smaller}
488
566
::::: {.columns}
489
567
::: {.column .incremental width="55%"}
490
568
* You can loop over any iterable
@@ -530,6 +608,41 @@ for index, animal in enumerate(my_list):
530
608
:::
531
609
:::::
532
610
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
+
533
646
## Question {.smaller}
534
647
::: {.fragment .fade-in }
535
648
* Create a list of integers from 0 to 5
@@ -568,48 +681,49 @@ print(sum_of_squares)
568
681
```
569
682
:::
570
683
571
- ## Conditional statements {.smaller}
684
+ ## While loops {.smaller}
572
685
::::: {.columns}
573
686
::: {.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
576
690
:::
691
+
577
692
::: {.column width="45%"}
578
693
:::: {.fragment .fade-in}
579
694
``` {python}
580
695
#| echo: true
581
696
#| 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!")
592
704
```
593
705
::::
594
706
:::
707
+
595
708
:::::
596
709
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
601
710
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
+ :::
602
718
::: {.fragment .fade-in}
603
719
``` {python}
604
720
#| echo: true
605
721
#| 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
613
727
```
614
728
:::
615
729
0 commit comments