When you need to choose among more than two distinct options (such as calculating university student letter grades, categorizing income tax brackets, or dispatching HTTP request methods), chaining multiple if statements becomes unwieldy. The elif (short for else if) statement creates a clean, multi-way decision ladder.
if condition_1:
statement_block_1
elif condition_2:
statement_block_2
elif condition_3:
statement_block_3
else:
fallback_default_block
# Program: Calculate University Grade (BCA Syllabus)
percentage = 83.5
if percentage >= 90:
grade = "A+ (Outstanding)"
elif percentage >= 80:
grade = "A (Excellent)"
elif percentage >= 70:
grade = "B+ (Very Good)"
elif percentage >= 60:
grade = "B (Good)"
elif percentage >= 50:
grade = "C (Average)"
elif percentage >= 40:
grade = "P (Pass)"
else:
grade = "F (Fail / Re-appear)"
print(f"Percentage: {percentage}% | Result: {grade}")
Because Python short-circuits the ladder at the first matching True condition, you must always structure conditions in strict descending or ascending order:
# Common Beginner Bug:
score = 95
# WRONG ORDER:
if score >= 50:
print("Pass") # BUG: This executes for 95, preventing >= 90 from ever running!
elif score >= 90:
print("Distinction")
else block is optional. However, including an else block is considered a best practice to catch unexpected or unhandled default cases.