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).

On Start: the program has a temperature. 2 branches to choose from.

Watch out: Think about 35. 35 > 20 and 35 > 10 would be True as well, yet the program never gets that far. The first True condition (35 > 30) wins and everything after it is skipped.

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.

6 of 6 still to sort.

The six comparisons: == equal to · != not equal to · < less than · <= less than or equal to · > greater than · >= greater than or equal to.

Python · = or ==

Find the faulty line

This program should greet anyone called Alex. One line has a slip in it. Which one?

A student's program — which line goes wrong?

Python · and, or, not

Does a 70-year-old get the discount?

A cinema program gives a discounted ticket when this condition is True: age < 12 or age > 60. A 70-year-old steps up to the till.

Which of these is closest to what you think?
How sure are you?

Python · Syntax and nesting

Finish the program

Build a program that prints one of two messages. Then you'll see an if tucked inside it. Fill each gap with the correct line.

  1. score = 70The program starts by giving score the value 70. That's a single = doing assignment.
  2. missing step
Which line is step 2?

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

1Only one path runs: the first True condition wins, and the conditions after it are not checked.
2Order matters in an elif chain, because swapping two conditions can send the same input down a different path.
3Inside a condition you compare with ==. A single = is for assigning a value.
4and, or and not turn several True-or-False questions into one answer.
5Colons and indentation show which block each line belongs to, even when selection is nested.

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?
When a program has more than one possible path and chooses one by checking a condition that is True or False.
What is a condition?
An expression that evaluates to True or False, like 5 > 10 (False) or 10 == 10 (True).
== != < <= > >= : what do they mean?
Equal to, not equal to, less than, less than or equal to, greater than, greater than or equal to.
= or ==: which one compares two values?
==. A single = assigns a value.
If and else together: how many of the two blocks run, and what decides it?
Exactly one. The condition decides: True runs the if block, False runs the else block.
An if on its own, and its condition is False. What happens?
Its block is skipped and the program carries on.
How does multi-path selection use its conditions?
It checks them one by one and follows the first one that is True, skipping the rest.
How many elif blocks can you use? Is else needed?
As many elif blocks as you need. The else and its block are optional.
How do you write if, elif and else in Python?
All lowercase, and each one is followed by a colon.
Why indent the block after the colon?
Indentation shows which code belongs to which if, elif or else block.
What does and do?
It is True only when both expressions are True.
What does or do?
It is True when either expression, or both, is True.
What does not do?
It is True when its expression is False.
What is a nested if?
An if and its block placed inside another if's block. The inner block must be indented correctly.

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 posted

More KS3 Computer Science topics

See the full KS3 Computer Science curriculum →

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.

Helpful guides for parents and students