How does Python make decisions with if, elif and else, and how do we build conditions?
Use Python selection with if, elif and else, build conditions with comparison and logical operators, and order branches correctly
A focused answer to the O-Level Computing point on Python selection. Using if, elif and else, comparison operators, combining conditions with and, or and not, and ordering branches so each value falls into the right one.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this dot point is asking
SEAB wants you to use selection in Python with if, elif and else, to build conditions with comparison and logical operators, and to order branches so every value goes into the intended one. The central idea is that selection lets a program choose what to do based on a condition, and the order and combination of conditions decide which branch runs.
The answer
if, elif and else
Selection chooses between blocks of code:
if temperature > 30:
print("Hot")
elif temperature > 20:
print("Warm")
else:
print("Cool")
Python tests the conditions top to bottom. The first true condition runs its block, and the rest are skipped. elif (else if) adds more cases; else catches everything not matched above. Indentation (four spaces) shows which lines belong to each branch.
Comparison operators
A condition usually compares two values:
| Operator | Meaning |
|---|---|
== |
equal to |
!= |
not equal to |
> |
greater than |
< |
less than |
>= |
greater than or equal to |
<= |
less than or equal to |
Note == tests equality; a single = assigns a value, which is a common slip.
Combining conditions
Logical operators join or reverse conditions:
and: true only if both sides are true.or: true if either side is true.not: reverses a condition.
if age >= 13 and age <= 19:
print("Teenager")
Ordering branches
When ranges meet, order matters. Check the most specific or highest band first, so a value is caught by the right branch:
if mark >= 75: # checked first
grade = "A"
elif mark >= 60: # only reached if below 75
grade = "B"
If you checked >= 60 first, a mark of would wrongly be labelled "B".
Examples in context
Example 1. A ticket price calculator. A cinema program uses if/elif/else on age to set a child, standard or senior price. Checking the bands in order, youngest first, ensures each customer gets exactly one correct price with no overlap.
Example 2. Validating a menu choice. A program checks if choice == 1 ... elif choice == 2 ... else: print("Invalid"). The final else handles any unexpected input, so a wrong key gives a clear message rather than silently doing nothing.
Try this
Q1. State the difference between = and == in Python. [2 marks]
- Cue.
=assigns a value to a variable;==tests whether two values are equal.
Q2. Write a condition that is true when score is at least and at most . [2 marks]
- Cue.
if score >= 50 and score <= 100:.
Q3. Explain why the order of if and elif branches matters with overlapping ranges. [2 marks]
- Cue. The first true branch runs, so a broad condition placed too early captures values meant for a later, more specific branch.
Exam-style practice questions
Practice questions written in the style of SEAB exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
Original6 marksWrite a Python program that reads a mark out of and prints a grade: 'A' for and above, 'B' for to , 'C' for to , and 'Fail' below .Show worked answer →
Use an if/elif/else chain, checking the highest band first:
mark = int(input("Enter mark: "))
if mark >= 75:
print("A")
elif mark >= 60:
print("B")
elif mark >= 50:
print("C")
else:
print("Fail")
Because the branches are checked top to bottom, once mark >= 75 is false we know the mark is below before testing >= 60, so each range is covered exactly once.
Markers reward converting the input to an int, the correct boundaries (, , , else), and ordering the conditions from highest to lowest.
Original5 marks(a) Write a condition that is true only when a number `n` is between and inclusive. (b) Write a condition that is true when a character `c` is 'y' or 'Y'. (c) State what the operator `not` does in a condition.Show worked answer →
(a) Combine two comparisons with and:
if n >= 1 and n <= 10:
Both parts must be true, so n must be at least and at most .
(b) Combine two equality tests with or:
if c == "y" or c == "Y":
Either match makes the condition true.
(c) not reverses a condition: not (x > 5) is true exactly when x > 5 is false (that is, when x <= 5).
Markers reward and for the inclusive range, or for the two characters, and not reversing a condition's truth value.
Related dot points
- Declare and use Python variables, identify the core data types (int, float, str, bool), and convert between them
A focused answer to the O-Level Computing point on Python variables. Assigning variables, the core data types int, float, str and bool, reading input as a string, and converting between types with int(), float() and str().
- Use Python for loops with range and while loops with a condition, including accumulators and avoiding infinite loops
A focused answer to the O-Level Computing point on Python loops. Using for with range for a known count, while with a condition for an unknown count, building accumulators, and avoiding infinite loops.
- Define and call Python functions with parameters and return values, and explain the benefits of using functions
A focused answer to the O-Level Computing point on Python functions. Defining functions with def, passing parameters, returning values, the difference between a parameter and an argument, and why functions make programs reusable.
- Use Python lists and strings, including indexing, length, looping over items, and basic operations like append and slicing
A focused answer to the O-Level Computing point on Python lists and strings. Creating lists, zero-based indexing, finding length with len(), looping over items, appending, and basic string operations and slicing.
- Use the IF function for conditional results and lookup functions such as VLOOKUP to find values in a table
A focused answer to the O-Level Computing point on logical and lookup functions. Using IF for conditional results, nesting IF for grades, and VLOOKUP to find a matching value in a table.