Python 3.14 ·
✓ verified by execution on 2026-07-21
So far your programs have run every line, top to bottom, no matter what. The if statement is how you give a program a choice: run this block only when a condition is true. It’s the single most common way to make code react to its input.
python
x = 5if x > 0: print('Positive')
Output
Positive
Your output
Adding an else branch
python
x = -2if x > 0: print('Positive')else: print('Non-positive')
Output
Non-positive
Your output
Chaining with elif
When you have more than two cases, chain them with elif (short for “else if”). Python checks each condition in order and runs the first block whose condition is true — then skips the rest. Both elif and else are optional.
python
x = 0if x > 0: print('Positive')elif x == 0: print('Zero')else: print('Negative')
is_raining = Trueif is_raining: print('Take an umbrella')print('Go outside')
Output
Take an umbrella
Go outside
Your output
Nested Conditionals
python
x = 10y = 5if x > 5: if y > 2: print('Both conditions met') else: print('Only first condition met')else: print('First condition not met')
Output
Both conditions met
Your output
Common Misconceptions
Myth: Python uses braces {} to define code blocks like C or Java.
Reality: Python uses indentation (whitespace) to mark what’s inside a block. Line up your code consistently — four spaces is the convention.
Myth: Every elif whose condition is true will run.
Reality: An if/elif chain stops at the first true condition, runs that one block, and skips all the rest.
Myth: The colon : at the end of an if line is optional.
Reality: The colon is required — it marks the start of the indented block. Leaving it off raises a SyntaxError.
Under the Hood
Watch how execution jumps directly past unmatched branches:
Indentation (whitespace) — Python strictly uses indentation to group code blocks together.
If you have an if-elif chain and the first if is True, will Python check the elif?
Reveal answer
No, it stops at the first true condition. — Python stops evaluating as soon as it finds the first true condition in an if-elif chain.
Which keyword is used for 'else if' in Python?
Reveal answer
elif — Python uses 'elif' to avoid excessive indentation.
Challenges
Challenge 1 +50 XP
Write an if-elif-else chain that prints 'Even' if a number `n` is even and 'Odd' if it is odd.
python
Test 1 — expects "Even"
Need a hint? (−25% XP)
Use the modulo operator % to check for even numbers.
Show solution (0 XP)
n = 4if n % 2 == 0: print('Even')else: print('Odd')
Challenge 2 +100 XP
Given a temperature `t`, print 'Hot' if t > 30, 'Warm' if t > 20, and 'Cold' otherwise.
python
Test 1 — expects "Warm"
Need a hint? (−25% XP)
Check the highest temperature first.
Show solution (0 XP)
t = 25if t > 30: print('Hot')elif t > 20: print('Warm')else: print('Cold')
Go deeper
Play the Cache: Why LRU Sometimes Scores Zero — A cache has to decide what to throw away before it knows what will be asked for next. Take the eviction decision yourself here, scored live against LRU and against the best any policy could possibly do.