KS3 · Computer Science
Selection in Python: IF, ELIF, ELSE
It's 15 degrees: hot, warm or cool? A program can't say "cool-ish". It needs True-or-False questions, and a rule for which to trust when several say True.
Python · Selection
Follow one temperature through the program
The program is: if temperature > 30, then elif temperature > 20, then elif temperature > 10, then else. Start with temperature = 15. At each signpost, pick True or False and follow that route. Then go back and try 35, 25 and 5.
1st check: if temperature > 30 → 2nd check: elif temperature > 20 → 3rd check: elif temperature > 10
Your first signpost is if temperature > 30. Is it True or False for 15?
4 possible routes (the program only ever takes one).
Predict, then check
Same program, but someone has shuffled the order. Commit to an answer first.
The checks now run in this order: (1) if temperature > 10 prints "It's cool"; (2) elif temperature > 20 prints "It's warm"; (3) elif temperature > 30 prints "It's hot"; else prints "It's cold". The temperature is 25. What gets printed?
Python · Conditions
True or False?
Decide before you peek: does each condition come out True or False?
Still to sort
True (0)
The comparison is correct for these values.
Where the line is: True means correct for these exact values. 'Nearly right' is still False.
False (0)
The comparison is not correct for these values.
Where the line is: A condition is never 'maybe': it ends up as exactly True or exactly False.
The six comparisons: == equal to · != not equal to · < less than · <= less than or equal to · > greater than · >= greater than or equal to.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
A program at a fork takes exactly one path, and the first True condition wins.
What you need to know
- Selection is a fork in the road: the program checks a condition, which is True or False, then follows a path.
- A condition is an expression that comes out True or False, such as 5 > 10 (False) or 10 == 10 (True).
- The six comparisons are == equal to, != not equal to, < less than, <= less than or equal to, > greater than, >= greater than or equal to.
Have a goQuick one before you scroll on: x is 4. Does x >= 4 come out True or False? And x != 4?
x >= 4 is True. x != 4 is False.
4 is equal to 4, so 'greater than or equal to' holds. But != asks whether the values are different, and they aren't.
- A single = assigns a value, while == compares two values. So a condition about name is name == "Alex".
Have a goA program has the line score = 20. Is that comparing score with 20, or doing something else?
Something else: it assigns 20 to score.
A single = is assignment. Comparing would need ==, as in score == 20.
- With if and else, exactly one block runs: the if block when the condition is True, the else block when it is False.
- An if on its own just skips its block when the condition is False, and the program carries on.
- Add elif for three or more options: conditions are checked one by one and the first True one wins. else is optional.
Have a goSam is sure that when x is 8, a program with if x > 5 and then elif x > 1 prints BOTH messages, since both are True. Is Sam right?
No. Only the if message prints.
The first True condition wins: x > 5 is True, so x > 1 is never checked, even though it would be True too.
- if, elif and else are lowercase and each is followed by a colon; indenting the block shows which lines belong to it.
- Logical operators join conditions: and needs both True, or needs either (or both), and not is True when its expression is False.
Have a goWork it out: what does not (5 > 10) come out as, True or False?
True.
5 > 10 is False, and not is True when its expression is False.
- A nested if sits inside another if's block, so its own block must be indented correctly too.
The big picture
Selection lets a Python program choose one path. It checks conditions that come out True or False, built with relational operators like == and > and joined with and, or and not. With if, elif and else it follows the first True condition. Colons and indentation show which code belongs to which block, and selection can be nested inside other selection.
Key points
Worked example
Problem
A badge program checks its conditions in this order: if points >= 90 prints "Gold"; elif points >= 50 prints "Silver"; elif points >= 20 prints "Bronze"; else prints "No badge". Trace it for points = 60. (The badge names are made up for this example.)
⚠ Watch out
Thinking the program picks the condition that fits best, or runs every block whose condition is True. Multi-path selection checks the conditions one by one and takes the first True one, so the order you write them in matters.
Memory hook
Signposts: walk down the conditions, turn at the first True, and ignore everything after it.
Check yourself
Without scrolling up: a program checks if x > 8, then elif x > 3, then else. Trace x = 5, then x = 9. Which conditions get checked, and which block runs each time?
Flashcards
(14)What is selection?
What is a condition?
== != < <= > >= : what do they mean?
= or ==: which one compares two values?
If and else together: how many of the two blocks run, and what decides it?
An if on its own, and its condition is False. What happens?
How does multi-path selection use its conditions?
How many elif blocks can you use? Is else needed?
How do you write if, elif and else in Python?
Why indent the block after the colon?
What does and do?
What does or do?
What does not do?
What is a nested if?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore KS3 Computer Science topics
How this lesson was checked. This KS3 Computer Sciencelesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 7 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.