3.2.2, 3.2.4, 3.2.5 - Selection and Boolean conditions (Python)
Selection lets a program choose an action from its data. You will build Python conditions from comparisons and Boolean operators, then use them to control clearly indented branches.
Comparing values
A comparison produces a Boolean value, True or False, that a program can use to make a decision.
| Meaning | Python | Example when score is 10 |
|---|---|---|
| Equal to | == | score == 10 is True |
| Not equal to | != | score != 10 is False |
| Less than | < | score < 10 is False |
| Greater than | > | score > 8 is True |
| Less than or equal to | <= | score <= 10 is True |
| Greater than or equal to | >= | score >= 10 is True |
Inclusive comparisons accept the boundary itself. A pass rule of at least 10 requires score >= 10; score > 10 would reject a score of exactly 10. Python's = assigns a value, while == compares values. AQA supplied pseudocode uses = for equality and the symbols ≠, ≤ and ≥; translate their meaning into the native Python operators when writing a Python answer.
Combining Boolean conditions
Boolean operators join or reverse comparisons so that one condition can express a whole rule.
Python spells them and, or and not. and is true only when both conditions are true. or is true when at least one is true, including when both are true. not reverses a Boolean value.
age = 14
has_permission = True
is_suspended = False
allowed = (age >= 12 and age <= 16) and has_permission
print(allowed and not is_suspended)
The two age comparisons are true, permission is true and suspension is false, so this displays True. An out-of-range age is described by age < 12 or age > 16: being below the minimum or above the maximum is sufficient. age < 12 and age > 16 can never be true for one number. These same conditions can control iteration, for example repeating a request while an age is out of range. Brackets make the intended grouping clear when several operators appear together.
Choosing which statements run
An if statement runs its indented body only when its condition is true.
Selection, alongside sequence and iteration, is a basic principle for combining statements. Python ends the condition line with a colon and uses indentation to show the branch body. An else supplies the alternative. With several alternatives, elif tests another condition only if the preceding one was false.
score = 7
if score >= 8:
print("Gold")
elif score >= 5:
print("Silver")
else:
print("Keep practising")
print("Result recorded")
For score 7, the gold condition is false and the silver condition is true. Only Silver is selected; Result recorded then runs because it is outside the branches. The order matters: testing score >= 5 first would capture a score of 9 before a later gold test. Independent if statements could run more than one body, which would be inappropriate if exactly one award is required.
The boundary of a pass
{"code":"score = 10\nif score >= 10:\n print("Pass")\nelse:\n print("Retry")","instructions":"Predict and run for score 10. Change >= to > and run again. Explain which written rule each condition implements."}