Python elif Statement

Ka Kavitha V Updated 03 Oct 2026
5 min read ·Lesson 20 of 23

elif Statement

Often a decision has more than two possible outcomes. A grade can be A, B, C, or F. A traffic light can be red, yellow, or green. A menu can offer several options. The elif statement lets you check several conditions in sequence and run the block that belongs to the first condition that is true.

What Is elif?

elif is short for "else if". It adds another condition to an if statement.

  • If the if condition is False, Python checks the first elif condition.
  • If that is also False, Python checks the next elif, and so on.
  • As soon as one condition is True, its block runs and all remaining conditions are skipped.

Syntax

if condition1:
    # runs if condition1 is True
elif condition2:
    # runs if condition1 is False and condition2 is True
elif condition3:
    # runs if the conditions above are False and condition3 is True
else:
    # optional: runs if none of the conditions are True

An elif must always follow an if (or another elif); it cannot appear on its own.

Basic Example of elif

x = 45
y = 60

if x > y:
    print("x is greater than y")
elif x < y:
    print("x is smaller than y")

Expected output:

x is smaller than y

Explanation:

  1. The if condition checks whether x is greater than y: 45 > 60 is False.
  2. Python moves on and evaluates the elif condition: 45 < 60 is True.
  3. The elif block runs and prints the message.

What happens if x and y are equal? Both conditions are False, so nothing is printed. You can handle that case with another elif x == y: or with an else block.

Using Multiple elif Conditions

You can include any number of elif statements. Python checks them in order, from top to bottom, and runs the first one that matches.

Example: Student Result Evaluation

marks = 68

if marks >= 85:
    print("Distinction")
elif marks >= 70:
    print("First Class")
elif marks >= 55:
    print("Second Class")
elif marks >= 40:
    print("Pass")

Expected output:

Second Class

How Python evaluates it:

StepConditionResult
168 >= 85False — move on
268 >= 70False — move on
368 >= 55True — print "Second Class" and stop
468 >= 40Not checked

Notice that 68 >= 40 is also true, but it is never evaluated. Only the first true condition runs.

How elif Execution Works

Python follows these rules:

  1. Conditions are checked from top to bottom.
  2. At most one block is executed.
  3. Once a condition is true, the rest are skipped.

Even if several conditions could be true, only the first one runs. This is why the order of conditions matters.

Why Order Matters

Compare the correct order above with this incorrect version:

marks = 92

if marks >= 40:
    print("Pass")
elif marks >= 85:
    print("Distinction")

Expected output:

Pass

A score of 92 deserves "Distinction", but marks >= 40 is checked first and is already true, so the more specific condition is never reached. When conditions use ranges, check the most restrictive condition first.

Adding an else Block

An else block at the end catches every case that none of the conditions matched.

Example: Age-Based Classification

user_age = 32

if user_age < 12:
    print("Child category")
elif user_age < 18:
    print("Teen category")
elif user_age < 60:
    print("Adult category")
else:
    print("Senior category")

Expected output:

Adult category

32 < 12 and 32 < 18 are both False; 32 < 60 is True, so "Adult category" is printed.

Why else is useful:

  • It catches all remaining cases.
  • It guarantees that exactly one block runs, so the program always produces a result.

In the student-results example earlier, a mark below 40 would print nothing. Adding else: print("Fail") would cover that case.

When Should You Use elif?

Use elif when:

  • The conditions are mutually exclusive — only one outcome makes sense.
  • Only one block should execute.
  • You want clear and efficient logic.

elif vs Multiple if Statements

Using separate if statements instead of elif causes every condition to be checked, and can run more than one block:

marks = 92

if marks >= 85:
    print("Distinction")
if marks >= 70:
    print("First Class")
if marks >= 55:
    print("Second Class")

Expected output:

Distinction
First Class
Second Class

All three conditions are true, so all three messages print — which is wrong for a grading system. With elif, only "Distinction" would print, and the remaining checks would be skipped.

Featureif / elif chainSeparate if statements
Blocks that can runAt most oneAny number
Conditions checkedUntil the first true oneAll of them
Best forMutually exclusive choicesIndependent checks

Example: Menu Selection Program

option = 4

if option == 1:
    print("Start Game")
elif option == 2:
    print("Load Game")
elif option == 3:
    print("Settings")
elif option == 4:
    print("Exit")
else:
    print("Invalid option")

Expected output:

Exit

Explanation:

  • Only one menu option is executed.
  • Python stops checking as soon as option == 4 matches.
  • The else block handles any number outside 1–4.

Tip: For long chains that compare one value against many fixed options, Python 3.10 and later also offer the match statement, covered in the Python Match lesson.

Practical Example: Electricity Bill Slabs

units = 230

if units <= 100:
    rate = 3.0
elif units <= 200:
    rate = 4.5
elif units <= 300:
    rate = 6.0
else:
    rate = 8.0

bill = units * rate
print(f"Units: {units}, Rate: {rate}, Bill: {bill}")

Expected output:

Units: 230, Rate: 6.0, Bill: 1380.0

Each branch assigns a value to rate. After the chain finishes, rate is used in the calculation. Because every possible value of units reaches exactly one branch, rate is always defined.

Common Mistakes

MistakeProblem
Writing else ifSyntaxError — Python uses elif
Putting a broad condition before a narrow oneThe narrow condition is never reached
Using separate if statements for exclusive choicesSeveral blocks may run
Forgetting an else for unexpected valuesSome inputs produce no result
Writing elif without a preceding ifSyntaxError
  • Python if Statement — the basics of conditional execution
  • Python else Statement — handling the "otherwise" case
  • Python Match — pattern matching for many fixed options
  • Python Nested If — decisions inside decisions

0 Comments

Reviewed before they appear

No comments yet.

Python
Ask about this post
AI Ask about this post

Ask questions about Python elif Statement and get answers drawn from it.

Signed-in readers only.