Conditional Statements
Python's conditionals are minimal and readable. elif not else if, no parentheses required around conditions, and match/case (Python 3.10+) for pattern matching.
mark = 74
if mark >= 80:
print("Distinction")
elif mark >= 70:
print("Merit — Upper")
elif mark >= 60:
print("Merit")
elif mark >= 50:
print("Pass")
else:
print("Not yet competent")
# No parentheses needed around the condition (unlike Java/C#)
# elif not "else if"
Ternary Expression
# Python ternary: value_if_true if condition else value_if_false
result = "Pass" if mark >= 50 else "Fail"
print(result)
# Nested ternary — avoid for readability:
grade = ("Distinction" if mark >= 80
else "Merit" if mark >= 60
else "Pass" if mark >= 50
else "Fail")
print(grade)
match/case (Python 3.10+)
Python's structural pattern matching is more powerful than a simple switch — it matches structures, types, and values simultaneously.
day = "Wednesday"
match day:
case "Monday":
print("Java fundamentals — 09:00 to 13:00")
case "Tuesday":
print("Data types and operators")
case "Wednesday":
print("Career development and CV writing")
case "Thursday":
print("Project work and peer review")
case "Friday":
print("Presentations and feedback")
case _: # default
print("Self-study recommended")
# Match with guards:
match mark:
case n if n >= 80:
print(f"{n}% — Distinction")
case n if n >= 50:
print(f"{n}% — Pass")
case _:
print("Not yet competent")
Practice Task
Your Turn
Write a program with a hardcoded mark and a boolean for whether the learner submitted on time. Use if/elif/else to assign a grade label. Use the ternary operator for a one-line pass/fail string. Add a nested if for late submission with a penalty message. Use match/case for the day-of-week schedule.
Common Mistakes
- Python uses elif, not else if (two words causes SyntaxError).
- Forgetting the colon after if, elif, else.
- Wrong indentation in the elif chain.
- match/case requires Python 3.10+ — check with python --version.
Professional Tip
The ordering of conditions in an if/elif chain matters critically. Always put the most restrictive condition first. A mark of 90 hitting elif mark >= 50 first would be wrongly labelled Pass.
Mini Quiz
Python's keyword for additional conditions after if is?