-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditional_logic.qmd
More file actions
67 lines (44 loc) · 1.93 KB
/
conditional_logic.qmd
File metadata and controls
67 lines (44 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
---
filters:
- pyodide
---
# Conditional Logic
{{< video https://youtu.be/9Fix--fIVEc >}}
In the Principles of Programming section, we talked about Conditional Logic.

In Python, we apply conditional logic using IF / ELIF / ELSE statements. Let’s see how it works.
Let’s imagine we want to check someone’s age to determine whether they should be sent to room A or room B of our clinic :

:::{.callout-tip}
Note the tab indentations - these are important as they indicate a block of code. In this case, there are two blocks of code - one to run if the patient age is less than 60, one to run if that’s not the case. In this case, each block is a single line of code, but blocks can be as long as we need.
:::


Let's try running this with some different age variables to see the impact!
```{pyodide-python}
age = 12
if age < 16:
print("Please go to room A")
else:
print("Please go to room B")
```
```{pyodide-python}
age = 55
if age < 16:
print("Please go to room A")
else:
print("Please go to room B")
```
If we provide an invalid option - here we provide a name as a string instead - then running the code will give us an error because it doesn't know how to compare a string (the name) to an integer (the bit on the right of our comparison operator (16)).
```{pyodide-python}
age = "Bob"
if age < 16:
print("Please go to room A")
else:
print("Please go to room B")
```
## Comparison Operators
There are many comparison operators in Python. We use these to express relational statements that resolve to a Boolean - True or False. They are therefore used in conditional logic.

You won't be able to play along, but have a watch of this game of 'comparison operators' to learn more about comparisons.
{{< video https://youtu.be/wAAy0S96bks >}}