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.
score = 68
if score >= 70:
print("High")
elif score >= 40:
print("Pass")
else:
print("Low")- Which branch will run?
- Why are the later branches skipped once a true condition is found?
- What would be printed if
scorewere 35?
Investigate
Use this code to explain exactly how the program works.
temperature = 5
raining = True
if temperature < 10 and raining:
print("Coat")
else:
print("No coat")- Evaluate the two comparisons inside the condition.
- How does
andaffect the decision? - What changes if
rainingbecomesFalse?
Two-way selection
age = 15
if age < 16:
print("Child ticket")
else:
print("Standard ticket")Child ticket
- The condition after
ifis evaluated first. - If it is True, the indented
ifblock runs. - Otherwise the
elseblock runs.
More than two choices
Use one or more elif branches when several mutually exclusive outcomes are needed.
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 Exam Reference Language ↔ Python
| OCR Exam Reference Language / term | Python | What to remember |
|---|---|---|
if condition then ... endif | if condition: | Start a selection. |
elseif condition then | elif condition: | Additional condition checked only if earlier branches were false. |
else | else: | Fallback branch. |
AND / OR / NOT | and / or / not | Boolean operators inside conditions. |
Worked example
Testing the highest boundary first prevents a high score from being captured by a lower band.
score = int(input("Score: "))
if score >= 80:
print("Gold")
elif score >= 60:
print("Silver")
else:
print("Bronze")Silver
- Python checks
score >= 80first. - If that is false, it checks
score >= 60. - For 72 the second condition is true, so
Silveris printed. - The
elsebranch is skipped once a true branch has been executed.
Another example
A selection condition can combine more than one requirement.
age = 15
ticket = "yes"
if age >= 14 and ticket == "yes":
print("Entry allowed")
else:
print("Entry denied")Entry allowed
- Both comparisons must be true because they are joined with
and. - The string comparison uses
==, not assignment=.
Common mistakes
- Testing a lower threshold before a higher one in a banded
elifchain. - Forgetting the colon at the end of Python
if,elifandelselines. - Using
=instead of==in a comparison. - Writing OCR
elseifdirectly in Python; Python useselif.
endif; Python uses indentation instead. Indentation is part of Python syntax.Exam-ready checklist
- 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,elifandelse. - Conditions are Boolean expressions.
- Indentation shows which statements belong to each branch.