We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

Python If, Elif, and Else Statements: What's the Difference?

Lane Wagner
Lane WagnerBoot.dev co-founder and backend engineer

Last published

Table of Contents

Every useful program needs to make decisions. Python's if statement is how you tell your code to do one thing or another depending on some condition. Once you understand if, elif, and else, you can handle just about any branching logic.

All the content from our Boot.dev courses are available for free here on the blog. This one is the "Comparisons" chapter of Learn Python for Beginners. If you want to try the far more immersive version of the course, do check it out!

if, elif, and else at a glance

A chain starts with one if, can have any number of elif branches in the middle, and can end with one else. Python checks the conditions from top to bottom and runs the first branch whose condition is True. Everything after that branch is skipped. For example:

def player_status(health):
    if health <= 0:
        return "dead"
    elif health <= 5:
        return "injured"
    else:
        return "healthy"

The difference between elif and else is that elif checks a new condition, while else has no condition at all. It's the fallback that runs when nothing else matched.

Keyword Runs when How many per chain
if Its condition is True Exactly one, always first
elif Every earlier condition was False and its own is True Zero or more, in the middle
else Every if and elif condition was False Zero or one, always last

What are comparison operators in Python?

Click to play video

Before you write your first if statement, you need to know how to compare values. Boolean logic is the name for these kinds of comparison operations that always result in True or False.

Python has six comparison operators:

Operator Meaning
< less than
> greater than
<= less than or equal to
>= greater than or equal to
== equal to
!= not equal to
5 < 6  # True
5 > 6  # False
5 >= 6  # False
5 <= 6  # True
5 == 6  # False
5 != 6  # True

When a comparison happens, the result is a boolean value that you can store in a variable. These two lines do the same thing:

is_bigger = 5 > 4
is_bigger = True

Because 5 is greater than 4, is_bigger is assigned the value of True. You can store comparisons in variables like any other value.

How do if statements work in Python?

An if statement lets you run code only when a condition is True:

if CONDITION:
    # this code runs only when CONDITION is True

For example:

def show_status(boss_health):
    if boss_health > 0:
        print("Ganondorf is alive!")
        return
    print("Ganondorf is unalive!")

If boss_health is greater than 0, the function prints "Ganondorf is alive!" and returns early. Otherwise, it prints "Ganondorf is unalive!".

Without that return in the if block, both messages would print when the boss is alive:

def show_status(boss_health):
    if boss_health > 0:
        print("Ganondorf is alive!")
    print("Ganondorf is unalive!")

That would output:

Ganondorf is alive!
Ganondorf is unalive!

Indentation is what tells Python whether code belongs to the if block or not. And don't forget the colon (:) after your condition. It's required!

An if statement doesn't need an else. When the condition is False, Python skips the block and keeps going with whatever comes after it.

How does if/else work in Python?

An else block runs when the if condition is False. It gives you a clean two-way branch:

if score > high_score:
    print("High score beat!")
else:
    print("Better luck next time")

Some quick rules:

  • You can't have an else without an if
  • You can have an if without an else
  • else never has a condition of its own

The else block is optional, but when you need exactly two branches, it's cleaner than using a second if with the opposite condition. For simple two-way assignments, you can also use a ternary operator to write the whole thing on one line.

What is elif in Python?

elif stands for "else if". It lets you chain multiple conditions together:

if score > high_score:
    print("High score beat!")
elif score > second_highest_score:
    print("You got second place!")
elif score > third_highest_score:
    print("You got third place!")
else:
    print("Better luck next time")

Python evaluates these top to bottom. The first condition that's True has its body executed, and all the rest are skipped. If none of the if or elif conditions are True, the else block runs.

Back to the player_status function from the top of the article:

def player_status(health):
    if health <= 0:
        return "dead"
    elif health <= 5:
        return "injured"
    else:
        return "healthy"

Order matters. If you checked health <= 5 first, dead players (with health 0 or below) would be called "injured" instead.

What's the difference between elif and else?

elif and else both come after an if, and both only get a chance to run when the conditions above them were False. The difference is that elif brings its own condition to check, and else doesn't.

  • Use elif when there's another specific case to test, like health <= 5.
  • Use else for "everything that's left." It has no condition, so it catches every value the earlier branches didn't.

That's also why you can stack as many elif branches as you want but only one else. A second else in the same chain is a syntax error, because there's nothing left for it to catch.

Python doesn't have “else if”

If you're coming from JavaScript, C, or Java, you might reach for else if. In Python, that's a syntax error:

if score > 90:
    print("A")
else if score > 80:  # SyntaxError
    print("B")

elif is just Python's spelling of "else if".

Two if statements aren't the same as if/elif

Beginners often write back-to-back if statements when they mean if/elif. The difference is important when more than one condition could be True:

def player_status(health):
    status = "healthy"
    if health <= 5:
        status = "injured"
    if health <= 0:
        status = "dead"
    return status

Now, the version above happens to work because the later if overwrites the earlier one, but each if is checked independently. A player with health = 0 is "injured" for a moment and then "dead". With if/elif, Python stops at the first match, so only one branch ever runs. When the cases are mutually exclusive, use elif.

How do you combine multiple conditions with and/or?

Sometimes one condition isn't enough. Python's and and or operators let you combine boolean expressions.

and returns True only if both sides are True:

def is_dog(num_legs, weight):
    return num_legs == 4 and weight < 100

Let's trace through is_dog(4, 99):

return 4 == 4 and 99 < 100
return True and True
return True

And is_dog(3, 98):

return 3 == 4 and 98 < 100
return False and True
return False

or returns True if at least one side is True:

def is_car_cool(speed, is_electric):
    return speed > 200 or is_electric

You can use parentheses to control the order of operations, like in math:

should_admit = (high_gpa and high_sat_score) or is_rich

What are guard clauses in Python?

When you have multiple conditions that must all be true, it's tempting to nest your if statements:

def check_conditions(condition_1, condition_2, condition_3):
    if condition_1:
        if not condition_2:
            if condition_3 > 1:
                return True
    return False

That's hard to read. I prefer to invert each condition and return early. These are called guard clauses:

def should_serve_drinks(age, is_working, time):
    if age < 21:
        return False
    if not is_working:
        return False
    if time < 5 or time > 10:
        return False
    return True

Each if block handles one reason to bail out. If you make it past all the guards, you know every condition was met. This pattern keeps your code flat and readable, with no deeply nested indentation to parse.

One more thing: if statements don't need an explicit comparison when checking booleans. These are identical:

if is_big:
    # ...

if is_big == True:
    # ...

Prefer the first. The == True is redundant.

What should you learn after Python conditionals?

Now that you can make your programs branch and decide, the next big step is loops, running code repeatedly until a condition changes. Loops and conditionals together are the foundation of all control flow. You'll also want to learn about lists soon, since loops and lists go hand in hand.

If you want to keep going through the full Python curriculum with hands-on exercises, check out the Learn Python for Beginners course on Boot.dev.

Frequently Asked Questions

What is the difference between elif and else in Python?

elif checks a new condition and runs only when every earlier condition was False and its own condition is True. else has no condition. It runs when every if and elif condition in the chain was False.

Does Python have an else if statement?

No. Writing else if on one line is a syntax error. Python spells it elif. You can nest an if inside an else block, but elif keeps the chain flat.

Can you have multiple elif statements in Python?

Yes. You can chain as many elif statements as you need between the if and the optional else. Python evaluates them top to bottom and runs the first one that is True.

What happens if no condition is True in an if/elif chain?

If there is an else block, that code runs. If there is no else, nothing happens and execution continues after the entire if/elif block.

Does an if statement need an else in Python?

No. An if statement can stand alone. When its condition is False, Python skips the block and continues with the code after it.

Related Articles