- Understand how selection lets a program make decisions
- Use comparison operators to build a condition
- Write
if,if/else, andif/elif/elsestatements
- I can write an
ifstatement that checks a condition - I can add an
elsebranch for when the condition is false - I can chain multiple conditions using
elif
Answer before the lesson begins. These check prior knowledge — it's fine if you're unsure.
1. "If it is raining, take an umbrella." If it is NOT raining, what happens?
2. Which symbol usually means "equal to" in maths?
3. True or false: a program can only ever do the exact same thing every time it runs, with no decisions.
Key vocabulary
True or False, used to decide which path a program takes.==, <, or >=.True.if condition was False.False.Selection: if / else
What is selection?
score = 42if blockelse blockOnly one branch runs for each decision. Trace the condition first, then follow the matching branch.
Selection is one of the three basic building blocks of any program, alongside sequence (running instructions in order) and iteration (repeating instructions). Selection lets a program choose between different paths depending on whether a condition is true or false. Without selection, every program would do exactly the same thing every single time it ran, regardless of any input.
Comparison operators
A condition is built using a comparison operator, which compares two values and produces a boolean result (True or False):
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 3 | True |
< | Less than | 3 < 5 | True |
> | Greater than | 3 > 5 | False |
<= | Less than or equal to | 5 <= 5 | True |
>= | Greater than or equal to | 4 >= 5 | False |
Notice that == (two equals signs) checks whether two values are equal, while a single = is used for assignment — storing a value in a variable. These do two completely different jobs, and mixing them up is one of the most common beginner errors.
The if statement
An if statement runs a block of code only when its condition is True. In Python, the structure is: if condition: followed by an indented block underneath. The colon and the indentation are both required — Python uses indentation (not brackets, unlike many other languages) to show which lines belong inside the if.
if / else and if / elif / else
An else branch runs when the if condition was False, covering "everything else." When there are more than two possible paths, elif ("else if") checks an additional condition, and you can chain as many elif branches as needed before an optional final else. Python checks each condition in order from top to bottom and runs only the first block whose condition is True — it does not check any further conditions after that.
Worked examples
A program checks whether someone is old enough to vote.
age = 16if age >= 18: — the condition is checked.print("You can vote.") — this indented line only runs if the condition above was True. Since age is 16, nothing is printed here.A program checks whether a test score is a pass or a fail.
score = 42if score >= 50:print("Pass")else:print("Fail")Since 42 is less than 50, this program outputs
Fail.A program assigns a grade based on a percentage score.
score = 72if score >= 90: print("A")elif score >= 70: print("B")elif score >= 50: print("C")else: print("Fail")B is printed and the remaining conditions are never checked.A program offers a discount to customers: if age < 12: print("Child discount"), elif age >= 65: print("Senior discount"), else: print("Full price").
Answer the following:
- What is printed if
ageis 8? - What is printed if
ageis 70? - What is printed if
ageis 30?
Child discount— 8 is less than 12, so the first condition isTrue.Senior discount— 70 is not less than 12, but it is greater than or equal to 65.Full price— 30 fails both theifandelifconditions, so theelsebranch runs.
= instead of ==. if age = 18: is invalid — a single = is for assignment, not comparison. Python will raise a SyntaxError.if. Missing it, or mixing tabs and spaces, causes an IndentationError.:. Every if, elif, and else line must end with a colon before the indented block.if statements instead of elif. Using several independent if statements means Python checks every single one, even after an earlier condition matched — this can cause more than one message to print when only one was intended.N4 pupils: when explaining selection in your evidence, describe the flow — which condition is checked first, what happens if it's true, and what happens if it's false — rather than just describing what each line does in isolation.
Questions 1–5 are auto-checked. Questions 6–7 are self-marked — write your answer, then reveal the model answer to check your work. Questions 8–9 are practical tasks in IDLE.
1. Which symbol checks if two values are equal in Python? TYPE 1
2. Given score = 45, what does if score >= 50: print("Pass") else: print("Fail") output? TYPE 1
3. What causes an IndentationError in Python? TYPE 1
4. In an if / elif / else chain, what happens once Python finds a condition that is True? TYPE 1
5. Given temp = 30, which branch runs in if temp > 25: print("Hot") elif temp > 15: print("Mild") else: print("Cold")? TYPE 1
6. Explain the difference between = and == in Python, using an example of each. TYPE 2 N4
= is the assignment operator — it stores a value in a variable, for example age = 13 stores 13 in age. A double == is the equality comparison operator — it checks whether two values are equal and produces a boolean result, for example age == 13 checks whether age currently holds the value 13, without changing it.7. Explain why writing three separate if statements instead of if / elif / else can cause a program to behave unexpectedly. TYPE 2 N3
if statements are each checked independently, so Python tests every single one even after an earlier condition has already matched. If more than one condition happens to be true at once, this could cause more than one message to print, when the programmer only intended one branch to run. Using elif guarantees only one branch in the chain ever runs.8. Practical: In IDLE, create a new file called sdd3_selection.py. Write a program that asks the user for a test score (as an integer) and prints "Pass" if it is 50 or above, or "Fail" otherwise. Test it with 65 and with 30. Record your output below. TYPE 3
score = int(input("Enter your score: "))
if score >= 50:
print("Pass")
else:
print("Fail")Expected output:
Pass for 65, Fail for 30.9. Practical: In sdd3_selection.py, extend your program to give a grade instead: "A" for 90+, "B" for 70+, "C" for 50+, or "Fail" otherwise, using elif. Test with 95, 75, 55, and 20. Record your output. TYPE 3
if score >= 90:
print("A")
elif score >= 70:
print("B")
elif score >= 50:
print("C")
else:
print("Fail")Expected output:
A, B, C, Fail respectively.Suggested timing: 55 minutes. Warm up 5 min; notes 15 min; examples 10 min; now you try 5 min; task set 20 min.
Key misconception to address: = vs == confusion is near-universal at this stage — drill it explicitly with side-by-side examples.
Live demo suggestion: live-code the grade-bands example, then change the score value and re-run repeatedly so pupils see the flow change.
Extension idea (S5 / N5-bridging, non-assessed): briefly show combining conditions with and/or as a "look ahead" — keep clearly separate from the assessed task set.
Assessment standards covered: N3 O2.1; N4 O1.2, O2.1.