One of the most powerful ways to utilize computation is to respond to differences in the context our code is running in by changing the behavior of our code. This might sound complicated, but as a human you are very familiar with this kind of response.
Conditional behavior
There are many situations where you change your behavior based on the environment. For example, if it is raining outside, you wear rain boots. Otherwise, you might wear a different kind of shoe like tennis shoes.
You probably have even more complicated environmental responses as well. For example, if you are ordering bubble tea, your order might go like this: if the shop has taro, you get taro in your tea. If the shop doesn’t have taro but has pearl, you get pearl in your tea. Finally, in the case that the shop doesn’t have taro or pearl, you don’t order any bubble tea.
In computer science, we call this kind of behavior conditional: your code runs only in the case that some condition is satisfied.
Conditions
In computer science, conditions are binary. They are either true or false, never inbetween. To create these conditions, we can use comparison operators to compare values.
💻 Guess what condition the following comparisions will evaluate to. You can test your guesses by typing these conditions into the Python shell:
2 < 3
2 == 3
"Gold" == "Silver"
"Gold" == "Gold"
Python has the following comparison operators: <, >, ==, <=, >=, !=.
✏️ Using the Python shell to experiment, create a table like the one below that defines what each of the operators does and gives an example of when the operator will generate a True condition and when the operator will generate a False condition:
| Operator | Description | True condition example | False condition example |
|---|---|---|---|
< |
|||
> |
|||
== |
|||
<= |
|||
>= |
|||
!= |
Conditionals
Using the conditions generated by comparison operators, you can conditionally execute pieces of your code. This is useful for changing what your code does to respond to different condtions of the program.
if statements
if statements are the begining to every conditional code block. The code written inside the code
block that follows only runs if the condition after the if evaluates to True.
for i in range(20):
if i < 10:
print("Smaller than 10")
else statements
else statements can be paired with if statements to create an alternative block of code to
execute if the condition after the if evaluates to False.
What is the difference between the following two programs?
for i in range(20):
if i < 10:
print("Smaller than 10")
print("Greater than or equal to 10")
for i in range(20):
if i < 10:
print("Smaller than 10")
else:
print("Greater than or equal to 10")
elif statements
Finally, elif statements (“else if”) can be used to create multiple branches of a conditional.
These statements add another condition to check if the condition above them does not pass.
The following program creates three branches of exectution:
for i in range(20):
if i < 10:
print("Smaller than 10")
elif i < 15:
print("Greater than 9 but less than 15")
else:
print("Greater than or equal to 15")
This conditional creates the folloing cases for the variable i:
- i < 10
- 10 <= i < 15
- 15 <= i