Learn

Short lessons with real code you can edit and run on the page.

if, elif and else in Python

Lesson 10 Python 3.14 Runs in your browser Updated
In short

An if statement runs a block of code only when its condition is true. elif adds another condition to test when every condition before it fails, and else catches everything left over, so exactly one block in the chain runs.

Key facts

  • In an if statement, the condition line ends with a colon, and the block under it is indented, four spaces by convention.
  • In an if / elif / else chain, Python stops at the first condition that is true.
  • elif is one word. else if is a syntax error in Python.
  • Almost any value can be a condition. False, None, zero and empty things count as false, and almost everything else as true.

How does an if statement work in Python?

Write if, a condition, and a colon, then indent the lines that should run when the condition is true. Python checks the condition once, runs the block or skips it, and continues with the first line after the block.

Python
temperature = 5
if temperature < 10:
    print("Wear a coat")
print("Have a good day")
Output
Wear a coat
Have a good day

Change the temperature to 15 and run it again. The coat line disappears and the last line still prints, because it sits outside the block.

What is the difference between if, elif and else?

if starts the chain, elif adds another condition to try, and else runs when nothing before it was true. Python tests from the top and stops at the first match, so one block runs and the rest are skipped.

Python
score = 85
if score >= 90:
    print("A")
elif score >= 80:
    print("B")
elif score >= 70:
    print("C")
else:
    print("F")
Output
B

The order matters. With 85, score >= 70 is also true, but Python never gets there because score >= 80 matched first. Put the strictest condition at the top.

Why use elif instead of a second if?

Because separate if statements are all tested, while an elif is tested only when the ones before it failed. Run the example, which tests the same score both ways, and count the lines.

Python
score = 95
if score >= 90:
    print("A")
if score >= 80:
    print("B")
print("---")
if score >= 90:
    print("A")
elif score >= 80:
    print("B")
Output
A
B
---
A

Two ifs gave the student two grades. The chain gave one. Use elif when only one outcome should happen, and separate ifs when every true condition should run its own block.

Can I write if else on one line?

Yes, when the point is to choose a value. The form is value_if_true if condition else value_if_false, and Python calls it a conditional expression.

Python
age = 20
status = "adult" if age >= 18 else "minor"
print(status)
Output
adult

Keep it for short choices. Once the two branches do more than produce a value, the four-line form is easier to read.

How do I nest if statements?

Indent an if inside another. Each level of indentation is one level of nesting, and each else belongs to the if at the same indentation.

Python
logged_in = True
is_admin = False
if logged_in:
    if is_admin:
        print("Admin panel")
    else:
        print("Dashboard")
else:
    print("Please log in")
Output
Dashboard

Two levels are fine. At three or more, combine the conditions with and, which the next lesson covers. Later in the course, functions give you another way out.

Can I use a string or list as a condition?

Yes. Python treats empty strings, empty collections, zero, False and None as false and almost everything else as true, so if name: is the usual way to check that a value is present.

Python
name = ""
if name:
    print(f"Hello, {name}")
else:
    print("Hello, stranger")
items = [1, 2]
if items:
    print(f"{len(items)} items")
Output
Hello, stranger
2 items

Common mistakes with if statements in Python

Each of these four mistakes with if statements gets an error message that names the exact line. Read it before you reread the code.

  • IndentationError: expected an indented block after 'if' statement. The line under the if isn't indented. Add four spaces.
  • SyntaxError: expected ':'. The colon at the end of the condition is missing.
  • Writing else if. Python reports it as a missing colon after else. The keyword is elif.
  • A single = in the condition. Python suggests == in the error message. Comparison uses two equals signs.
Python
x = 3
if x > 2:
print("big")
Output
  File "main.py", line 3
    print("big")
    ^^^^^
IndentationError: expected an indented block after 'if' statement on line 2
Python
x = 1
if x == 2:
    print("two")
else if x == 1:
    print("one")
Output
  File "main.py", line 4
    else if x == 1:
         ^^
SyntaxError: expected ':'

Exercise

The grade chain is in the wrong order, so a score of 85 prints D. Reorder the branches, moving each condition together with the print line under it, so the program prints B. Do not change the score or the letters.

Python
score = 85
if score >= 60:
    print("D")
elif score >= 70:
    print("C")
elif score >= 80:
    print("B")
else:
    print("F")
Output
Show the solution
score = 85
if score >= 80:
    print("B")
elif score >= 70:
    print("C")
elif score >= 60:
    print("D")
else:
    print("F")

Quiz

This quiz has 5 questions. Pick an answer to see why it is right or wrong.

  1. 1What does this program print?

    x = 10
    if x > 5:
        print("big")
    elif x > 8:
        print("bigger")
    else:
        print("small")
  2. 2How do you write "else if" in Python?

  3. 3What does this program print?

    name = ""
    print("yes" if name else "no")
  4. 4What does Python report when the line after an if is not indented?

  5. 5When should you use separate if statements instead of elif?

Frequently asked questions

What is the difference between if, elif and else in Python?
if tests the first condition, elif tests another condition only when the ones before it failed, and else runs when none of them were true. At most one block in the chain runs, and exactly one when the chain ends with else.
Why use elif instead of if?
Separate ifs are all tested and several can run. An elif is skipped as soon as an earlier condition has matched, which is what you want when the conditions are alternatives.
When should elif be used?
When only one of several outcomes should happen, such as turning a score into one grade. Put the strictest condition first, because Python stops at the first match.
Can you write if else on one line in Python?
Yes, with a conditional expression, as in status = "adult" if age >= 18 else "minor", where the part after = picks one of the two values. It produces a value, so it goes anywhere a value can, such as the right side of an assignment or inside print().
How do I check multiple conditions in one if statement?
Put and between two conditions when both must be true, as in if age >= 18 and has_ticket. Put or between them when one is enough, and not in front of a condition to reverse it. The logical operators lesson covers all three.
Is else if valid in Python?
No. Python reports else if as a missing colon. The keyword is elif, one word.