09-selection.py
python-programming / programming-constructs  ·  Lesson 10
Lesson 10 — Programming constructs

Selection

Choose which block of code runs by testing conditions.

Selection lets a program take different paths. Python uses if, elif and else.

Predict

Read the code first. Do not run it yet.

selection-predict.py
score = 68
if score >= 70:
    print("High")
elif score >= 40:
    print("Pass")
else:
    print("Low")
Predict before running
  • Which branch will run?
  • Why are the later branches skipped once a true condition is found?
  • What would be printed if score were 35?

Investigate

Use this code to explain exactly how the program works.

selection-investigate.py
temperature = 5
raining = True
if temperature < 10 and raining:
    print("Coat")
else:
    print("No coat")
Investigate the code
  • Evaluate the two comparisons inside the condition.
  • How does and affect the decision?
  • What changes if raining becomes False?

Two-way selection

ticket.py
age = 15
if age < 16:
    print("Child ticket")
else:
    print("Standard ticket")
Output
Child ticket
How it works
  • The condition after if is evaluated first.
  • If it is True, the indented if block runs.
  • Otherwise the else block runs.

More than two choices

Use one or more elif branches when several mutually exclusive outcomes are needed.

Indentation is part of PythonThe statements controlled by if, elif or else must be indented consistently.

Self-study guide

Selection lets a program choose which instructions to execute. The condition is evaluated as True or False, and that result controls the path through the code. Python uses if, elif and else; OCR Exam Reference Language uses if ... then, elseif ... then, else, endif.

Conditions can use comparisons and Boolean operators. The order of branches matters because an if / elif / else chain stops at the first condition that is true.

OCR focusUse selection practically and understand two-way and multi-way decisions. Be able to translate between Python selection and OCR IF-THEN-ELSE structure.

OCR Exam Reference Language ↔ Python

OCR Exam Reference Language / termPythonWhat to remember
if condition then ... endifif condition:Start a selection.
elseif condition thenelif condition:Additional condition checked only if earlier branches were false.
elseelse:Fallback branch.
AND / OR / NOTand / or / notBoolean operators inside conditions.
Read both formsIn an examination you may see OCR Exam Reference Language even when you have learned the idea in Python. Translate the structure, not just the symbols.

Worked example

Testing the highest boundary first prevents a high score from being captured by a lower band.

lesson-10-worked.py
score = int(input("Score: "))

if score >= 80:
    print("Gold")
elif score >= 60:
    print("Silver")
else:
    print("Bronze")
Output
Silver
Work through it line by line
  • Python checks score >= 80 first.
  • If that is false, it checks score >= 60.
  • For 72 the second condition is true, so Silver is printed.
  • The else branch is skipped once a true branch has been executed.

Another example

A selection condition can combine more than one requirement.

lesson-10-example-2.py
age = 15
ticket = "yes"

if age >= 14 and ticket == "yes":
    print("Entry allowed")
else:
    print("Entry denied")
Output
Entry allowed
Why this example matters
  • Both comparisons must be true because they are joined with and.
  • The string comparison uses ==, not assignment =.

Common mistakes

Avoid these errors
  • Testing a lower threshold before a higher one in a banded elif chain.
  • Forgetting the colon at the end of Python if, elif and else lines.
  • Using = instead of == in a comparison.
  • Writing OCR elseif directly in Python; Python uses elif.
Exam-safe warningOCR ERL closes the structure with endif; Python uses indentation instead. Indentation is part of Python syntax.

Exam-ready checklist

You should now be able to
  • Write two-way and multi-way selection.
  • Choose appropriate conditions and branch order.
  • Translate elseif ↔ elif.
  • Use Boolean operators inside selection conditions.
  • Trace exactly which branch runs for a given input.

What you have learned

  • Selection chooses between different paths.
  • Python uses if, elif and else.
  • Conditions are Boolean expressions.
  • Indentation shows which statements belong to each branch.