Skip to content
Laviyx Learn

Try

Python course menu

Python elif: Check Several Conditions in Order

Learn Python if-elif-else for multiple conditions: grading systems, ranges, why the order of conditions matters, when else is optional, and the match statement as an alternative.

Beginner7 min readUpdated
On this page

Some decisions have more than two outcomes: a grade can be A, B, C or F; a ticket can be child, adult or senior. elif (short for "else if") lets you check several conditions in order, and runs the block of the first one that is true.

Syntax#

Python
if condition1:
    ...
elif condition2:
    ...
elif condition3:
    ...
else:
    ...            # none of the above

You can have as many elifs as you need. else at the end is optional.

Example: grades#

Python
score = 82

if score >= 90:
    grade = "A"
elif score >= 75:
    grade = "B"
elif score >= 60:
    grade = "C"
else:
    grade = "F"

print(f"Score {score}: grade {grade}")
Output
Score 82: grade B

Python checks top to bottom. 82 >= 90 is false, 82 >= 75 is true, so grade = "B" and the rest is skipped, even though 82 >= 60 is also true.

Trying several scores:

Python
for score in [95, 82, 64, 40]:
    if score >= 90:
        grade = "A"
    elif score >= 75:
        grade = "B"
    elif score >= 60:
        grade = "C"
    else:
        grade = "F"
    print(score, grade)
Output
95 A
82 B
64 C
40 F

Order matters#

Because only the first true condition runs, putting a broad condition first breaks the logic:

Python
score = 95

if score >= 60:
    grade = "C"
elif score >= 75:
    grade = "B"
elif score >= 90:
    grade = "A"

print(grade)
Output
C

95 is greater than 60, so the first branch wins and the others are never checked. Put the most specific (or highest) condition first.

Notice too that the elif score >= 75 line does not need and score < 90. Earlier branches have already caught higher scores.

Ranges: a BMI category#

Python
bmi = 23.4

if bmi < 18.5:
    category = "Underweight"
elif bmi < 25:
    category = "Normal"
elif bmi < 30:
    category = "Overweight"
else:
    category = "Obese"

print(category)
Output
Normal

Without else#

If no condition matches and there is no else, nothing happens. That is fine when "no action" is a valid outcome, but if a variable is only set inside the branches, make sure some branch always runs (usually with else).

Alternative: match (Python 3.10+)#

When you compare one value against several fixed options, match can be clearer than a long elif chain:

Python
day = "sat"

match day:
    case "sat" | "sun":
        print("Weekend")
    case "mon" | "tue" | "wed" | "thu" | "fri":
        print("Weekday")
    case _:
        print("Unknown day")
Output
Weekend

| means "or", and case _ catches everything else, like else. For ranges and complex conditions, stick with if/elif.

Common mistakes#

Practice#

Practice exercise

An electricity bill is charged in slabs:

  • first 100 units: ₹3 per unit
  • units 101–200: ₹5 per unit
  • above 200: ₹8 per unit

Compute the bill for 80, 150 and 320 units.

Show hint

For 150 units: 100 × 3 for the first slab, plus (150 − 100) × 5 for the second.

Show solution
Python
for units in [80, 150, 320]:
    if units <= 100:
        bill = units * 3
    elif units <= 200:
        bill = 100 * 3 + (units - 100) * 5
    else:
        bill = 100 * 3 + 100 * 5 + (units - 200) * 8
    print(units, bill)
Output
80 240
150 550
320 1760

For 320 units: 300 + 500 + 120 × 8 = 1,760.

Key takeaway#

Finished this lesson?