Unit 2 • Lesson 4

if-else and elif Structures

Overview

Expand on simple if logic by handling alternative outcomes using else and multiple cases with elif. You'll learn how to write branching logic that responds dynamically to different scenarios.

Beginner 15–20 min

What You Will Learn in This Lesson

By the end of this lesson, you will know:

  • The else clause: How to handle cases when the if condition is False.
  • The elif clause: How to check multiple conditions in sequence.
  • When each block runs: Understanding the order of evaluation.
  • Practical examples: Using if-else-elif to handle complex decision-making.

Why This Matters

Simple if statements only handle one case. With else and elif, you can handle multiple scenarios and ensure your program always takes some action, no matter what the conditions are.

Step 1: The else Clause

The else clause lets you specify what happens when the if condition is False. It's like saying "if this is true, do that; otherwise, do this other thing."

Basic if-else Structure
if condition:
    # code to run if condition is True
    print("Condition is true!")
else:
    # code to run if condition is False
    print("Condition is false!")
1

Start with if

Write your if statement as usual with a condition and colon.

2

Add the else Clause

After the if block, add else: on a new line at the same indentation level as if.

3

Indent the else Block

All code that should run when the condition is False must be indented under else.

Important: The else clause doesn't have a condition — it automatically handles all cases where the if condition is False. Also, else must be at the same indentation level as if, not indented under it.

Mini Practice #1: Using else

Try It Yourself

Try this if-else statement. Notice how it always prints something:

Press Run to see output

What happened? Python first checked the if condition: age >= 18. Since age is 20, and 20 is greater than or equal to 18, the condition is True. When the if condition is True, Python runs the if block and prints "You are an adult!", then skips the else block entirely. If you change age to 15, the if condition would be False (15 is not >= 18), so Python would skip the if block and run the else block instead, printing "You are a minor." The else clause ensures that something always happens - either the if block runs OR the else block runs, but never both.

Step 2: The elif Clause

The elif clause (short for "else if") lets you check multiple conditions in sequence. Python checks each condition from top to bottom and runs the first block where the condition is True.

if-elif-else Structure
if condition1:
    # runs if condition1 is True
    print("First case")
elif condition2:
    # runs if condition1 is False AND condition2 is True
    print("Second case")
else:
    # runs if both conditions are False
    print("Default case")

How elif Works

Python checks conditions in order. Once it finds a True condition, it runs that block and skips the rest.

score = 85
if score >= 90:
    print("A")
elif score >= 80:
    print("B")  # This runs!
elif score >= 70:
    print("C")  # Skipped

Multiple elif Clauses

You can have as many elif clauses as you need. They're checked in order.

temperature = 75
if temperature > 80:
    print("Hot")
elif temperature > 60:
    print("Warm")
elif temperature > 40:
    print("Cool")
else:
    print("Cold")

Key Concept: Only one block runs in an if-elif-else chain. Python stops checking conditions once it finds one that's True. This is why elif is better than multiple if statements when you only want one action to happen.

Mini Practice #2: Using elif

Try It Yourself

Try this if-elif-else chain. Notice how only one block runs:

Press Run to see output

What happened? Python checked conditions from top to bottom. First, it checked score >= 90 - this was False (85 is not >= 90), so Python moved to the next condition. Then it checked score >= 80 - this was True (85 is >= 80), so Python ran the elif block and printed "Grade: B". Once Python found a True condition, it stopped checking the remaining elif conditions and skipped the else block. This is why only "Grade: B" printed, even though 85 is also >= 70. The key point: Python stops at the first True condition in an if-elif-else chain, so order matters!

Step 3: Order Matters

The order of your conditions is important. Python checks them from top to bottom and stops at the first True condition.

✅ Correct Order
score = 85
if score >= 90:
    print("A")
elif score >= 80:
    print("B")  # Runs for 85
elif score >= 70:
    print("C")
else:
    print("F")
❌ Wrong Order
score = 85
if score >= 70:
    print("C")  # Runs first! Wrong!
elif score >= 80:
    print("B")  # Never reached
elif score >= 90:
    print("A")  # Never reached

Best Practice

Always check conditions from most specific to least specific, or from highest to lowest values. This ensures the correct block runs.

Step 4: When to Use Each Structure

Different structures are useful for different situations:

Use if alone

When you only care about one case. The program continues regardless.

if is_raining:
    print("Bring umbrella")

Use if-else

When you have exactly two cases and want to handle both.

if age >= 18:
    print("Adult")
else:
    print("Minor")

Use if-elif-else

When you have three or more cases and want exactly one action.

if score >= 90:
    print("A")
elif score >= 80:
    print("B")
else:
    print("Below B")

End-of-Lesson Exercises

Exercise 1: Write an if-else Statement

Create a variable age = 15. Write an if-else statement that prints "Adult" if age >= 18, otherwise prints "Minor".

Use if age >= 18: followed by else: with appropriate print statements.

Write your code above and click "Check Answer" to verify it's correct.

Exercise 2: Use elif

Create a variable score = 85. Write an if-elif-else statement that prints "A" if score >= 90, "B" if score >= 80, "C" if score >= 70, otherwise "F".

Use if, elif, and else to check conditions in order from highest to lowest.

Write your code above and click "Check Answer" to verify it's correct.