How to Use elif with else in Python: Absolute Beginners Tutorial

Python's elif and else keywords extend a basic if statement into a full decision chain. Once you understand how they work together, you can write programs that respond differently to many possible inputs — without writing the same check twice.

When your program needs to choose between more than two outcomes, a single if/else pair is not enough. elif — short for "else if" — lets you attach additional conditions to the same decision. Python evaluates them in order and executes only the first branch that matches. The optional else at the end catches everything that did not match any condition above it.

What elif Does and Why It Exists

An if statement tests one condition and branches into two outcomes: True or False. That covers a yes/no question, but programs often deal with three or more possibilities. Imagine assigning a letter grade to a numeric score — there are five possible grades, not two.

Without elif, you would need to write five separate if statements and ensure they do not conflict. elif solves that by chaining conditions onto the same block. Once one condition is True, Python runs that branch and skips the rest of the chain entirely.

Note

elif must always follow an if (or another elif). You cannot write elif on its own — Python will raise a SyntaxError.

Here is the grade-assignment example written as an if/elif chain:

python
score = 78

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
elif score >= 60:
    grade = "D"
elif score >= 0:
    grade = "F"

print(grade)  # C

Python tests score >= 90 first. It is False. Then it tests score >= 80 — also False. Then score >= 70True. Python sets grade = "C" and skips the remaining elif branches immediately.

Pro Tip

Order your elif conditions from most specific (or highest value) to least specific. Python stops at the first match, so a broad condition placed early can accidentally swallow cases meant for a narrower condition below it.

code builder click a token to place it

Build the first two lines of an elif branch that checks whether a score is 80 or above and assigns the grade "B":

your code will appear here...
grade = "B" else: elif grade = "A" score >= 80: if
Why: An elif branch starts with the keyword elif, followed by the condition and a colon (score >= 80:). The indented body on the next line ( grade = "B") runs only when that condition is True. else: has no condition and would not check the value of score. if would start a new independent check, not continue the chain.

Adding else as a Fallback

An elif chain handles the conditions you anticipate. But what about a value that does not match any of them? If no else is present and nothing matches, Python simply does nothing. Sometimes that is fine. Other times you need a guaranteed response for every possible input — and that is where else comes in.

else appears at the very end of an if/elif chain. It takes no condition of its own. Its block runs whenever every condition above it evaluated to False.

python
score = -5

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
elif score >= 60:
    grade = "D"
elif score >= 0:
    grade = "F"
else:
    grade = "invalid score"

print(grade)  # invalid score

With a score of -5, none of the numeric conditions are met. The else branch runs and assigns "invalid score". Without the else, the variable grade would never be defined, and a later print(grade) would raise a NameError.

"An else clause following elif acts as a catch-all for any case not covered above." — Python Software Foundation, Tutorial

What else Cannot Do

else does not accept a condition. Writing else score < 0: is a syntax error. If you need another specific condition, use another elif. Reserve else for the true fallback case — the one that covers everything you did not explicitly name.

Watch Out

A broad else can hide bugs. If your program reaches else with an unexpected value, it silently accepts it. Consider whether a specific elif with an explicit condition is safer than a blanket else.

spot the bug click the line that contains the error

One line in this code will cause a SyntaxError. Click it to select, then press check.

1 temperature = 15
2 if temperature > 30:
3 print("hot")
4 elif temperature > 20:
5 else temperature > 10:
6 print("mild")
7 else:
8 print("cold")
Explanation: Line 5 reads else temperature > 10:, which is invalid. else never takes a condition. If you want to check another specific value, use elif temperature > 10:. The else keyword on line 7 is the correct fallback and requires no condition.

elif vs Separate if Statements

A common early mistake is using a chain of separate if statements when elif is the right tool. The difference matters more than it might look.

With if/elif/else, Python evaluates conditions in order and stops at the first True result. Only one branch runs per execution. With separate if statements, every condition is tested independently, so multiple blocks could run if multiple conditions happen to be True.
Once an elif condition is True, Python skips all remaining elif and else blocks without evaluating their conditions. Separate if statements always evaluate every condition — useful when multiple checks are independent, but wasteful when they are mutually exclusive.
An else attached to an if/elif chain fires only when none of the conditions above it matched. An else attached to a standalone if fires whenever that single if is False — even if a previous, separate if already ran. These are very different behaviors.
Use separate if statements when the checks are independent and non-exclusive — for example, checking whether a user has admin rights AND whether the file exists as two completely separate questions. Use elif when you are choosing between mutually exclusive outcomes for the same variable or situation.

This example shows the difference in action:

python
x = 10

# Using elif — only ONE print executes
if x > 5:
    print("greater than 5")
elif x > 3:
    print("greater than 3")   # skipped — first branch already matched
elif x > 1:
    print("greater than 1")   # skipped

# Using separate if — MULTIPLE prints can execute
if x > 5:
    print("greater than 5")
if x > 3:
    print("greater than 3")   # also runs — separate check
if x > 1:
    print("greater than 1")   # also runs — separate check

How to Use elif with else in Python

Follow these steps to build a complete if/elif/else chain from scratch.

  1. Write the if condition

    Start with the if keyword, your condition, and a colon. Indent the block below it. This is the first — and highest-priority — test Python will evaluate.

  2. Add one or more elif clauses

    Immediately after the if block — at the same indentation level — write elif, your next condition, and a colon. Add as many elif branches as your logic requires. Python tests them in order and stops at the first match.

  3. Add an optional else clause

    After the final elif, write else: with no condition. Its indented block runs when every condition above was False. If you do not need a fallback, skip it — the chain is still valid without else.

  4. Verify only one branch executes

    Test your chain with a value that triggers the if, then one that triggers each elif, then one that falls through to else. Confirm that exactly one branch runs per test — if more than one output appears, you likely have a separate if where an elif was intended.

Python Learning Summary Points

  1. elif extends an if statement with additional conditions. Python evaluates them top to bottom and executes only the first branch whose condition is True, then skips the rest of the chain.
  2. else is an optional fallback at the end of an if/elif chain. It takes no condition and runs only when every condition above it was False. Without an else, no code runs if nothing matches.
  3. Use elif — not a chain of separate if statements — when your conditions are mutually exclusive. Separate if blocks all run independently, which can cause multiple branches to execute for the same input.

With if, elif, and else working together, your Python programs can handle any number of possible inputs cleanly and predictably. The key habit to build is reading your chain top to bottom and asking: what is the first condition this value will satisfy?

check your understanding question 1 of 5

Frequently Asked Questions

elif stands for "else if." It is a conditional clause that follows an if statement and provides an additional condition to test if the preceding if (or elif) was False. Python evaluates each condition in order and executes only the first branch whose condition is True.

The else clause acts as a fallback. It runs only when every condition above it — the if and all elif branches — evaluated to False. It does not take a condition of its own.

No. else is optional. You can write one or more elif branches without an else. Without else, if no condition matches, Python simply moves on and executes nothing from the chain.

Yes. A single if statement can be followed by as many elif clauses as needed. Python evaluates them in order and stops at the first True condition. Only one branch executes.

An elif is part of the same chain as the if above it — only one branch runs. A separate if is an independent check — both could run if their conditions are both True. Use elif when the conditions are mutually exclusive or when you want at most one branch to execute.

Python evaluates conditions top to bottom and stops at the first True result. Even if a later elif would also be True, it never gets checked once an earlier branch has already matched.

Yes. Like if and elif, the else keyword must be followed by a colon. The indented block below it contains the code that runs when no condition matched.

No. elif must always follow an if statement or another elif. Python will raise a SyntaxError if elif appears on its own without a preceding if.