The elif Ladder and Logical Conditions
15 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)When you have more than two possible outcomes, chaining multiple if statements can become messy. The elif (short for else if) ladder provides clean multi-way branching.
The elif ladder structure
Elif ladder is a chain made of an if, one or more elif lines and an optional else. Python checks the conditions from top to bottom and runs only the block of the first one that is True.
Python checks each condition in order from top to bottom. As soon as one condition evaluates to true, its block executes and the rest of the ladder is skipped.
score = 85if score >= 90: grade = "A"elif score >= 80: grade = "B"elif score >= 70: grade = "C"else: grade = "D"print(f"Grade: {grade}")Because score >= 80 evaluates to true, grade = "B" executes. Python never evaluates score >= 70.
Order of conditions matters
Always order your conditions from most specific to least specific. Placing general conditions first masks subsequent checks.
speed = 110if speed > 100: print("Severe speeding")elif speed > 80: print("Speeding")else: print("Normal speed")Example 1 · Shipping fee by weight
weight_kg = 12if weight_kg > 10: fee = 300elif weight_kg > 5: fee = 150else: fee = 50print(f"Fee: {fee}")Fee: 300
What this code does
- The biggest limit is checked first.
12 > 10is True, so fee is 300. - If
weight_kg > 5came first, 12 would match it and the 300 fee could never be reached.
If speed > 80 were placed before speed > 100, a speed of 110 would trigger 'Speeding' and never reach 'Severe speeding'.
Combining checks with logical operators
Logical operator is a word that joins or flips conditions. and needs both sides True, or needs at least one side True, and not turns True into False and False into True.
Use and and or within elif conditions to test multiple criteria simultaneously.
is_weekend = Falsehas_leave = Trueif is_weekend or has_leave: print("Office is closed")else: print("Working day")Example 2 · Role-based portal
role = "editor"is_authenticated = True
if not is_authenticated: print("Access denied: Login required")elif role == "admin": print("Admin Dashboard: Full privileges")elif role == "editor": print("Editor Portal: Content edit privileges")else: print("Viewer Mode: Read-only")Editor Portal: Content edit privileges
What this code does
- Checks authentication status first as a guard clause.
- Since
is_authenticatedis True androleis 'editor', the matching branch triggers.
Example 3 · Ticket price with two rules
age = 16is_weekend = True
if age < 5: price = 0elif age < 18 or age >= 60: price = 100else: price = 200
if is_weekend and price > 0: price += 20print(f"Ticket price: {price}")Ticket price: 120
What this code does
orlets one branch cover both children and seniors. 16 is under 18, so price is 100.- The second
ifis separate from the ladder.andneeds a weekend and a price above 0, so 20 is added.
Parentheses can be used to make complex multi-term boolean expressions unambiguous.
Next step
Check your understanding
8 quick questions