Why Conditionals Matter
Every non-trivial program needs to make decisions. if/elif/else is Python's way of branching โ executing different code depending on whether a condition is True.
Basic If Statement
score = 72
if score >= 50:
print("Pass")
If / Else
if score >= 50:
print("Pass")
else:
print("Fail")
If / Elif / Else โ Multiple Branches
if score >= 80:
grade = "A"
elif score >= 65:
grade = "B"
elif score >= 50:
grade = "C"
else:
grade = "F"
print(grade)
Only the first matching branch runs. Order matters โ always go from the most specific/highest to the lowest.
Nested Conditions
if age >= 18:
if has_licence:
print("Can drive")
else:
print("Get a licence")
else:
print("Too young")
Ternary Expressions (One-Line If)
status = "adult" if age >= 18 else "minor"
๐ฏ HKDSE tip: Examiners expect clean grading logic. Always cover every branch explicitly โ partial conditions lose marks.
Practise this on PyForm โ free
PyForm runs Python in your browser with an AI tutor trained for HKDSE. No install, no credit card.
Open PyForm โ