Phase 1: Prepare
Step 1 Warm-up recall
From M2.L2: what does else run on?
Private: nothing you type here is sent or stored.
Step 2 Learning goal
By the end of this lesson you can:
- write an if, elif, else chain (Foundation);
- combine conditions with and, or and not (Core);
- explain why the order of checks matters (Core).
Phase 2: Teach
Step 3 A familiar example
A grade report checks the highest band first: 80 or more is an A, otherwise 60 or more is a B, otherwise a C. The first match decides.
Step 4 Clear explanation
When there are more than two cases, add elif (else if) between if and else. Python checks from the top and runs only the first block whose condition is True.
score = 72
if score >= 80:
print("A")
elif score >= 60:
print("B")
else:
print("C")BOrder matters. If you checked score >= 60 first, a score of 85 would print B, because the first True wins. Check the narrowest or highest case first.
You can also join conditions:
| Expression | Meaning | Result when a is True and b is False |
|---|---|---|
| a and b | both must be True | False |
| a or b | at least one is True | True |
| not a | reverses the answer | False |
For example, if age >= 13 and age <= 19: is True only for ages 13 to 19.
Step 5 Worked example (4 marks)
What does this print when n = 15? if n > 20: print("big") elif n > 10: print("medium") else: print("small")
- 15 > 20 is False. (1 mark)
- 15 > 10 is True, so the elif block runs. (1 mark)
- The else block is skipped. (1 mark)
- Answer: medium. (1 mark)
Go deeper: the professor's notes
Next case.
Both true.
First True wins.
Flag
| Flag | Meaning |
|---|---|
| mode | one meaning only |
At work: A route flag
One flag chooses between two routines.
- Name each flag clearly.
- Test each value of the flag.
What a careful practitioner decides. Give each flag one meaning and test every value.
Phase 3: Practise
Step 6 Learner practice (instant-marked)
1. In an if, elif, else chain Python runs:
The first True wins.
2. The value of True and False is:
and needs both True.
3. The value of True or False is:
or needs one True.
4. The value of not True is:
not reverses.
Phase 4: Close
Step 7 Recap
- elif adds more cases; the first True block runs.
- The order of checks changes the result.
- and, or and not combine conditions.
Step 8 Exit ticket (ungraded)
Write a condition that is True only when age is from 13 to 19 inclusive.
Private: nothing you type here is sent or stored.
Step 9 Go further (Extension)
Write a grade program with four bands: A, B, C and F.
In industry: Knight Capital: a flag that picked old code
On 1 August 2012 Knight Capital repurposed a flag that had once switched on an old function. On one server the old code was still present, so orders with the flag ran the old function. In the first 45 minutes it sent more than 4 million orders to fill 212 customer orders, and the firm lost about 440 million dollars.
Link to this lesson. A condition decides which code runs. If the condition means something different on different machines, the program does the wrong thing at scale.
Source: SEC press release 2013-222; Fortune, 2 Aug 2012; Wikipedia, Knight Capital Group
Common mistakes
| Mistake | Why it fails |
|---|---|
| Checking the wide case first | A score of 85 would match >= 60 and never reach >= 80. |
| Writing age >= 13 and <= 19 | Repeat the variable: age >= 13 and age <= 19. |
| Using separate ifs by mistake | Separate ifs can all run; elif chooses one. |
Limits. More advanced forms, such as chained comparisons, exist but are not needed here.