Skip to main content
SingaporeComputer ScienceSyllabus dot point

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.

Generated by Claude Opus 4.87 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. The answer
  3. Examples in context
  4. Try this

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 8080 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 5050 and at most 100100. [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 100100 and prints a grade: 'A' for 7575 and above, 'B' for 6060 to 7474, 'C' for 5050 to 5959, and 'Fail' below 5050.
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 7575 before testing >= 60, so each range is covered exactly once.

Markers reward converting the input to an int, the correct boundaries (75\ge 75, 60\ge 60, 50\ge 50, 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 11 and 1010 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 11 and at most 1010.

(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