Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 140 additions & 26 deletions slides/basics.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,85 @@ 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")
```
:::
Comment on lines +478 to +495
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to go after for loops I think


## 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}
::: {.column .incremental width="55%"}
* Do the same thing multiple times
Expand Down Expand Up @@ -484,7 +562,7 @@ for i in range(5):
:::
:::::

## Loops {.smaller}
## For loops {.smaller}
::::: {.columns}
::: {.column .incremental width="55%"}
* You can loop over any iterable
Expand Down Expand Up @@ -530,6 +608,41 @@ 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
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
Expand Down Expand Up @@ -568,48 +681,49 @@ print(sum_of_squares)
```
:::

## Conditional statements {.smaller}
## While loops {.smaller}
::::: {.columns}
::: {.column .incremental width="55%"}
* Execute code only if a condition is met
* Use `if`, `elif` (else if), and `else`
* 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
#| 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!")
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}
* 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

## Question {.smaller}
<!-- exercise with while loop and break statement -->
::: {.fragment .fade-in }
* Write a loop that only prints numbers divisible by 3 until you get to 30
* But skip printing if the number is divisible by 5
* 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 and number % 5 != 0:
print(number)
number += 1
```
:::

Expand Down