Lesson 5 of 26 · about 14 min
Making decisions
if, elif and else let a program choose what to do. Conditions are True/False questions built with comparisons and and/or/not.
🧭 What you need first: Talking to the user
Every morning you make a tiny program in your head:
If it’s raining, take an umbrella. Otherwise, if it’s cold, take a coat. Otherwise, just go.
You check the questions in order and stop at the first one that’s true. If it’s raining and cold, you still only grab the umbrella, because you stopped at the first “yes.”
Programs make choices exactly the same way. The questions are called conditions, and they’re always answered with True or False.
Another way: a sorting machine at the post office
A parcel rolls down a belt. The first gate asks “heavier than 10 kg?” and if so, diverts it. The next gate asks “international?” and so on. Each parcel takes the first gate that says yes, and anything left over goes to the end of the belt. That’s if / elif / else.
Another way: a bouncer
A bouncer checks: “Are you 18 or over and on the list?” Both must be true to get in. A friendlier club might say “on the list or a member.” That’s how and and or combine conditions.
Follow the branches
Step through and watch which lines get skipped. Then press Edit the code, change temperature to 25 or 5, and visualize again.
Watch the code run, one step at a time
- 1temperature = 12
- 2raining = False
- 3if raining:
- 4 print("Take an umbrella")
- 5elif temperature < 15:
- 6 print("Take a coat")
- 7else:
- 8 print("Just go!")
- 9print("Have a nice day")
➜ yellow = the line about to run · ✓ = the line that just ran
Grown-up words
Comparison operators ask a question and give back a bool (True or False):
| Operator | Means | Example | Result |
|---|---|---|---|
== |
equal to | 3 == 3 |
True |
!= |
not equal to | 3 != 4 |
True |
< > |
less / greater than | 2 > 5 |
False |
<= >= |
or equal | 5 >= 5 |
True |
⚠️ The most common beginner bug: = assigns a value, == compares two values.
Logical operators combine conditions:
a and b: True only if both are True.a or b: True if at least one is True.not a: flips True ↔ False.
The shape of a decision:
if condition:
# runs when condition is True
elif other_condition:
# runs when the first was False and this one is True
else:
# runs when none of the above were True- The colon
:and the indentation (4 spaces) matter. Indented lines form a block that belongs to the line above. - Python checks top to bottom and runs only the first matching branch.
elifandelseare optional. You can have as manyelifs as you need.- Strings can be compared too:
"apple" == "Apple"isFalse(capital letters count).
Write a grader
Read a score (0–100). Set grade to 'A' for 90 and up, 'B' for 80–89, 'C' for 70–79, and 'Keep going' below 70. Print it. Try several scores in the input box.
Leap years
A year is a leap year if it’s divisible by 4, except years divisible by 100 aren’t, unless they’re also divisible by 400. (So 2024 ✅, 1900 ❌, 2000 ✅.)
Set is_leap to True or False for the year typed in. Test with 2024, 1900, 2000 and 2023.
x = 7. Which branch runs? if x > 5: A elif x > 3: B else: C
Got it?
Finish the lesson to color it in on your map and collect its concept cards.
Did this lesson click?