1. if / elif / else: the 3-branch decision pattern
Python evaluates an `if/elif/else` chain top-down and runs **only the first branch** whose condition is `True`. Subsequent `elif` tests are skipped even if they would also evaluate true. This top-down short-circuit is why ordering matters: write the most specific test first, the most general (`else`) last.
The expression after `if` does not need parentheses, but the colon at the end is mandatory. Indentation is syntactic, not cosmetic. Mixing tabs and spaces raises `TabError`. PEP 8 mandates 4 spaces per level; every modern editor (VS Code, PyCharm, Jupyter) inserts 4 spaces when you press Tab inside a `.py` file.
For exactly two cases, Python provides a **conditional expression** (ternary): `value if test else other`. This compresses three lines into one and is the idiomatic style for assigning a value based on a single check. Reserve `if/elif/else` blocks for branches that contain multiple statements or side effects.
# Run with: python branches.py
# Letter-grade conversion: a canonical CS50P assignment
score = 87
# Ordered most-specific to most-general
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
elif score >= 60:
grade = "D"
else:
grade = "F"
print(f"Score {score} -> Grade {grade}")
# Ternary form: use when both branches are single expressions
status = "pass" if score >= 60 else "fail"
print(f"Status: {status}")
# Chained comparison reads like math
if 80 <= score < 90:
print("solid B range")