Making Decisions with if / elif / else

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 = 5
if x > 0:
    print('Positive')
Output
Positive

Adding an else branch

python
x = -2
if x > 0:
    print('Positive')
else:
    print('Non-positive')
Output
Non-positive

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 = 0
if x > 0:
    print('Positive')
elif x == 0:
    print('Zero')
else:
    print('Negative')
Output
Zero
python
score = 85
if score >= 90:
    print('A')
elif score >= 80:
    print('B')
elif score >= 70:
    print('C')
else:
    print('F')
Output
B

Flow of Execution

python
is_raining = True
if is_raining:
    print('Take an umbrella')
print('Go outside')
Output
Take an umbrella
Go outside

Nested Conditionals

python
x = 10
y = 5
if 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

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:

python · visualize
score = 85
if score >= 90:
    print('A')
elif score >= 80:
    print('B')
elif score >= 70:
    print('C')
else:
    print('F')

Check yourself

What defines a code block in Python?

Reveal answer

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 = 4
if 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 = 25
if t > 30:
    print('Hot')
elif t > 20:
    print('Warm')
else:
    print('Cold')