Python selection: if, elif and else

Selection is one of the three core programming constructs in every UK GCSE Computer Science specification (alongside sequence and iteration). This unit covers Python's if / elif / else structure, the six comparison operators (==, !=, <, >, <=, >=), and combining conditions with the Boolean operators and, or and not. Every example runs in the browser so pupils can experiment with the conditions and see the branching in action. Perfect for classroom teaching, GCSE and A Level revision, and independent or home-schooled learners.


What you'll learn

  • Write single-branch if statements
  • Add an else branch for the 'otherwise' case
  • Chain conditions with elif
  • Use ==, !=, <, >, <=, >= to compare values
  • Combine conditions with and, or, not

UK GCSE topic coverage

  • Relational operators
  • Boolean values and naming
  • Selection with `if`
  • Two-branch selection
  • Multi-branch selection
  • Boolean logic (`and`, `or`, `not`)
  • Nested selection
  • Dry-run and trace tables
  • Selection with combined logic
  • Applied selection challenge

Worked example

Age check with elif

age = int(input("Age? "))
if age < 13:
    print("Child")
elif age < 18:
    print("Teenager")
else:
    print("Adult")

Try a full unit free with a RunPy account

Sign up free and get the whole of Unit 1 (Input, Output and Variables) with checked tasks and instant feedback, plus a selection of the exam-style questions so you can practice on real GCSE-style content before upgrading.


Lessons in this unit

Full interactive lessons with checked tasks are available to signed-in RunPy users. Unit 1 and a selection of exam-style questions are free with a RunPy account.

  1. Comparison operators and Boolean values
  2. Boolean expressions and storing results
  3. Introducing if statements
  4. if / else
  5. Using elif for multiple conditions
  6. Logical operators: and, or, not
  7. Nested if statements
  8. Tracing conditional logic
  9. Combining and/or with elif
  10. GCSE-style decision program

Frequently asked questions

What is the difference between = and == in Python?
= assigns a value to a variable (x = 5). == tests whether two values are equal (x == 5) and returns True or False. Mixing them up is the single most common early Python error.
Can I have more than one elif?
Yes. An if statement can have zero or many elif branches. Python checks them in order, top to bottom, and runs the first branch whose condition is True.
Which exam boards test selection?
All of them. Selection (if/elif/else) is a required construct in OCR J277, AQA 8525, Pearson Edexcel, Eduqas and WJEC GCSE Computer Science.