Conditional Statements (if / elif / else)¶
Conditional statements let your program decide what to do based on data.
The basic if¶
If the condition is True, the indented block runs. If False, it's skipped.
if + else¶
temperature = 32
if temperature > 30:
print("It's hot — drink water")
else:
print("Weather is fine")
if + elif + else¶
Use elif ("else if") for multiple conditions in order:
score = 78
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}")
Only the first matching branch runs. The rest are skipped.
Nested if¶
You can put if inside if:
age = 22
has_license = True
if age >= 18:
if has_license:
print("Can drive")
else:
print("Adult but no license")
else:
print("Too young to drive")
Combine conditions with and / or¶
Often cleaner than nesting:
age = 22
has_license = True
if age >= 18 and has_license:
print("Can drive")
elif age >= 18:
print("Adult but no license")
else:
print("Too young to drive")
One-liner: ternary expression¶
Same as:
Good for short choices. Avoid for complex logic — gets unreadable.
Truthy / Falsy values¶
In a condition, Python treats these as False:
- False
- None
- 0, 0.0
- Empty "", [], (), {}, set()
Everything else is True.
items = []
if items:
print("Has items")
else:
print("List is empty") # this runs
# Equivalent to:
if len(items) > 0:
print("Has items")
This lets you write cleaner code:
name = ""
# Pythonic
if not name:
print("Name is missing")
# Not as Pythonic
if name == "" or name is None:
print("Name is missing")
Common pitfall — == vs =¶
= is assignment. == is comparison.
x = 5
if x == 5: # comparison — correct
print("x is five")
# x = 5 inside an `if` is a SyntaxError in Python (good — protects you)
Example — leap year checker¶
A year is a leap year if: - divisible by 4 AND - NOT divisible by 100, OR - divisible by 400.
year = 2024
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(f"{year} is a leap year")
else:
print(f"{year} is NOT a leap year")
Try changing year to 1900, 2000, 2023 and re-run.
Practice¶
What does this print?
Expected: B
Print 'adult' when age is 18 or more
Expected: adult
Quiz — Quick check¶
What you remember
Q1. Which of these values is truthy in Python?
-
0 -
[] -
"" -
"False"
Why:
"False"is a non-empty string, so it's truthy. The string contains the letters F-a-l-s-e but Python only sees a non-empty sequence of characters.
Q2. What does this print?
-
Adult -
Minor -
True -
SyntaxError
Why: This is a ternary expression.
age >= 18isFalse(17 isn't ≥ 18), so theelsebranch is taken.
Q3. In an if / elif / elif / else chain, how many branches run?
- All branches whose condition is
True - Only the first branch whose condition is
True(orelseif none match) - Only the last branch whose condition is
True - Always just one —
else
Why: Python evaluates top-down and runs the first matching branch only. Subsequent branches are skipped, even if their conditions would also be
True.
Common doubts¶
Why use elif instead of stacking if after if?
elif is mutually exclusive — only one branch runs. Stacked ifs evaluate independently, so multiple branches can run. If you're picking one option from a set (like a grade), use elif. If you're applying multiple independent rules, use separate ifs.
Can I do if 1 < x < 10?
Yes! Python supports chained comparisons. 1 < x < 10 is the same as 1 < x and x < 10. It's clearer and slightly faster (x is only evaluated once).
How do I do a switch/case statement?
Python 3.10+ has match/case:
if/elif chain.