From 75585117c4f1854937a5db54442454ed227c044e Mon Sep 17 00:00:00 2001 From: lauraporta <29216006+lauraporta@users.noreply.github.com> Date: Tue, 30 Sep 2025 11:45:33 +0100 Subject: [PATCH 1/8] Include while loops and break and continue statements --- slides/basics.qmd | 165 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 126 insertions(+), 39 deletions(-) diff --git a/slides/basics.qmd b/slides/basics.qmd index 41e8e1f..3587ab5 100644 --- a/slides/basics.qmd +++ b/slides/basics.qmd @@ -449,7 +449,78 @@ print(my_dict['a_number']) ::: ::::: -## Loops {.smaller} +## Conditional statements {.smaller} +::::: {.columns} +::: {.column .incremental width="55%"} +* Execute code only if a condition is met +* Use `if`, `elif` (else if), and `else` +::: +::: {.column width="45%"} +:::: {.fragment .fade-in} +```{python} +#| echo: true +#| output-location: fragment +#| code-line-numbers: "|1|3|5|7,8|" + +porridge_temp = 45 + +if porridge_temp < 40: + print("Too cold!") +elif porridge_temp > 50: + print("Too hot!") +else: + print("Just right!") +``` +:::: +::: +::::: + +## Question {.smaller} +* Write a loop that goes through the numbers 0 to 10 +* For each number, print whether it is even or odd + + Hint: use the modulus operator `%` and `==` to check for evenness + +::: {.fragment .fade-in} +```{python} +#| echo: true +#| output-location: fragment +#| code-line-numbers: "|1|2,3|4,5|" + +for i in range(11): + if i % 2 == 0: + print(f"{i} is Even") + else: + print(f"{i} is Odd") +``` +::: + + +## While loops {.smaller} +::::: {.columns} +::: {.column .incremental width="55%"} +* Do something while a condition is met +* Use `while` +::: + +::: {.column width="45%"} +:::: {.fragment .fade-in} +```{python} +#| echo: true +#| output-location: fragment +expected_result = 10 +result = 0 +while not expected_result == result: + print("Not there yet...") + print(f"Result is {result}") + result += 1 +print("We got there!") +``` +:::: +::: + +::::: + +## For loops {.smaller} ::::: {.columns} ::: {.column .incremental width="55%"} * Do the same thing multiple times @@ -484,7 +555,7 @@ for i in range(5): ::: ::::: -## Loops {.smaller} +## For loops {.smaller} ::::: {.columns} ::: {.column .incremental width="55%"} * You can loop over any iterable @@ -530,6 +601,49 @@ for index, animal in enumerate(my_list): ::: ::::: +## Break and continue statements {.smaller} +::::: {.columns} +::: {.column .incremental width="55%"} +* Break out of a loop +* Continue to the next iteration +::: + +::: {.column width="45%"} +:::: {.fragment .fade-in} +```{python} +#| echo: true +#| output-location: fragment +for i in range(10): + if i == 5: + break + print(i) +``` +:::: + +:::: {.fragment .fade-in} +```{python} +#| echo: true +#| output-location: fragment + +``` +:::: + +:::: {.fragment .fade-in} +```{python} +#| echo: true +#| output-location: fragment +for i in range(10): + if i == 5: + continue + print(i) +``` +:::: +::: + +::::: + + + ## Question {.smaller} ::: {.fragment .fade-in } * Create a list of integers from 0 to 5 @@ -568,48 +682,21 @@ print(sum_of_squares) ``` ::: -## Conditional statements {.smaller} -::::: {.columns} -::: {.column .incremental width="55%"} -* Execute code only if a condition is met -* Use `if`, `elif` (else if), and `else` -::: -::: {.column width="45%"} -:::: {.fragment .fade-in} -```{python} -#| echo: true -#| output-location: fragment -#| code-line-numbers: "|1|3|5|7,8|" - -porridge_temp = 45 - -if porridge_temp < 40: - print("Too cold!") -elif porridge_temp > 50: - print("Too hot!") -else: - print("Just right!") -``` -:::: -::: -::::: - ## Question {.smaller} -* Write a loop that goes through the numbers 0 to 10 -* For each number, print whether it is even or odd - + Hint: use the modulus operator `%` and `==` to check for evenness - + +::: {.fragment .fade-in } +* Write a loop that only prints numbers divisible by 3 until you get to 30 +* Use a while loop +::: ::: {.fragment .fade-in} ```{python} #| echo: true #| output-location: fragment -#| code-line-numbers: "|1|2,3|4,5|" - -for i in range(11): - if i % 2 == 0: - print(f"{i} is Even") - else: - print(f"{i} is Odd") +number = 0 +while number < 30: + if number % 3 == 0: + print(number) + number += 1 ``` ::: From ac8a1a2e716ed6a62525b123ab9216e85df66860 Mon Sep 17 00:00:00 2001 From: Laura Porta Date: Tue, 30 Sep 2025 13:07:01 +0100 Subject: [PATCH 2/8] Update slides/basics.qmd Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com> --- slides/basics.qmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slides/basics.qmd b/slides/basics.qmd index 3587ab5..987a64c 100644 --- a/slides/basics.qmd +++ b/slides/basics.qmd @@ -604,7 +604,7 @@ for index, animal in enumerate(my_list): ## Break and continue statements {.smaller} ::::: {.columns} ::: {.column .incremental width="55%"} -* Break out of a loop +* `break` out of a loop * Continue to the next iteration ::: From 47545220248b060d0f75a4ee14d8eedbc4607b0f Mon Sep 17 00:00:00 2001 From: Laura Porta Date: Tue, 30 Sep 2025 13:07:09 +0100 Subject: [PATCH 3/8] Update slides/basics.qmd Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com> --- slides/basics.qmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slides/basics.qmd b/slides/basics.qmd index 987a64c..9ba66be 100644 --- a/slides/basics.qmd +++ b/slides/basics.qmd @@ -605,7 +605,7 @@ for index, animal in enumerate(my_list): ::::: {.columns} ::: {.column .incremental width="55%"} * `break` out of a loop -* Continue to the next iteration +* `continue` to the next iteration ::: ::: {.column width="45%"} From 30e4c94e32717ea876accc7da2dd45466fbbc237 Mon Sep 17 00:00:00 2001 From: Laura Porta Date: Tue, 30 Sep 2025 13:07:37 +0100 Subject: [PATCH 4/8] Update slides/basics.qmd Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com> --- slides/basics.qmd | 8 -------- 1 file changed, 8 deletions(-) diff --git a/slides/basics.qmd b/slides/basics.qmd index 9ba66be..2003714 100644 --- a/slides/basics.qmd +++ b/slides/basics.qmd @@ -620,14 +620,6 @@ for i in range(10): ``` :::: -:::: {.fragment .fade-in} -```{python} -#| echo: true -#| output-location: fragment - -``` -:::: - :::: {.fragment .fade-in} ```{python} #| echo: true From 699ef80b24fc92b1d6a19dd5c3ca4d084d360b75 Mon Sep 17 00:00:00 2001 From: Laura Porta Date: Tue, 30 Sep 2025 13:08:05 +0100 Subject: [PATCH 5/8] Update slides/basics.qmd Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com> --- slides/basics.qmd | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/slides/basics.qmd b/slides/basics.qmd index 2003714..ce18e0c 100644 --- a/slides/basics.qmd +++ b/slides/basics.qmd @@ -498,8 +498,9 @@ for i in range(11): ## While loops {.smaller} ::::: {.columns} ::: {.column .incremental width="55%"} -* Do something while a condition is met -* Use `while` +* What if you don't know when you should stop iterating? +* Use `while`! +* Does something while a condition is met ::: ::: {.column width="45%"} From 9c46a0d0480b40af19040eac29c02e7880b21b72 Mon Sep 17 00:00:00 2001 From: Laura Porta Date: Tue, 30 Sep 2025 13:08:22 +0100 Subject: [PATCH 6/8] Update slides/basics.qmd Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com> --- slides/basics.qmd | 1 + 1 file changed, 1 insertion(+) diff --git a/slides/basics.qmd b/slides/basics.qmd index ce18e0c..880a76f 100644 --- a/slides/basics.qmd +++ b/slides/basics.qmd @@ -679,6 +679,7 @@ print(sum_of_squares) ::: {.fragment .fade-in } * Write a loop that only prints numbers divisible by 3 until you get to 30 +* Skip printing if the number is divisible by 5 * Use a while loop ::: ::: {.fragment .fade-in} From 21d6128cc17fecba77204bb6ca4f673b0b7b37e2 Mon Sep 17 00:00:00 2001 From: lauraporta <29216006+lauraporta@users.noreply.github.com> Date: Tue, 30 Sep 2025 13:24:21 +0100 Subject: [PATCH 7/8] Invert order, change example --- slides/basics.qmd | 57 ++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/slides/basics.qmd b/slides/basics.qmd index 880a76f..ea8ea0e 100644 --- a/slides/basics.qmd +++ b/slides/basics.qmd @@ -495,32 +495,6 @@ for i in range(11): ::: -## While loops {.smaller} -::::: {.columns} -::: {.column .incremental width="55%"} -* What if you don't know when you should stop iterating? -* Use `while`! -* Does something while a condition is met -::: - -::: {.column width="45%"} -:::: {.fragment .fade-in} -```{python} -#| echo: true -#| output-location: fragment -expected_result = 10 -result = 0 -while not expected_result == result: - print("Not there yet...") - print(f"Result is {result}") - result += 1 -print("We got there!") -``` -:::: -::: - -::::: - ## For loops {.smaller} ::::: {.columns} ::: {.column .incremental width="55%"} @@ -675,11 +649,38 @@ print(sum_of_squares) ``` ::: +## While loops {.smaller} +::::: {.columns} +::: {.column .incremental width="55%"} +* What if you don't know when you should stop iterating? +* Use `while`! +* Does something while a condition is met +::: + +::: {.column width="45%"} +:::: {.fragment .fade-in} +```{python} +#| echo: true +#| output-location: fragment +expected_result = 10 +result = 0 +while not expected_result == result: + print("Not there yet...") + print(f"Result is {result}") + result += 1 +print("We got there!") +``` +:::: +::: + +::::: + + ## Question {.smaller} ::: {.fragment .fade-in } * Write a loop that only prints numbers divisible by 3 until you get to 30 -* Skip printing if the number is divisible by 5 +* But skip printing if the number is divisible by 5 * Use a while loop ::: ::: {.fragment .fade-in} @@ -688,7 +689,7 @@ print(sum_of_squares) #| output-location: fragment number = 0 while number < 30: - if number % 3 == 0: + if number % 3 == 0 and number % 5 != 0: print(number) number += 1 ``` From bb99a4abc65f6cabc97e029fb0816b2db27613e8 Mon Sep 17 00:00:00 2001 From: lauraporta <29216006+lauraporta@users.noreply.github.com> Date: Tue, 30 Sep 2025 13:31:28 +0100 Subject: [PATCH 8/8] Add `and` and `or` --- slides/basics.qmd | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/slides/basics.qmd b/slides/basics.qmd index ea8ea0e..419f210 100644 --- a/slides/basics.qmd +++ b/slides/basics.qmd @@ -494,6 +494,38 @@ for i in range(11): ``` ::: +## Using `and` and `or` {.smaller} +::::: {.columns} +::: {.column .incremental width="55%"} +* `and` and `or` are logical operators +* Used to combine conditions +::: + +::: {.column width="45%"} +:::: {.fragment .fade-in} +```{python} +#| echo: true +#| output-location: fragment +print(True and False) +print(True or False) +``` +:::: + +:::: {.fragment .fade-in} +```{python} +#| echo: true +#| output-location: fragment +ammount_of_rain = 0 +ammount_of_uv_rays = 10 + +if ammount_of_rain > 10 and ammount_of_uv_rays > 0: + print("Take umbrella and sunscreen!") +else: + print("Enjoy the sun!") +``` +:::: +::: +::::: ## For loops {.smaller} ::::: {.columns}