Chapter 16 of 20

if, elif & else in Python

In the previous topic, we learned how if lets a program make a decision. But what if we have multiple possible conditions?

For example, suppose we want to display:

  • "Excellent" if marks are 90 or above

  • "Good" if marks are 75 or above

  • "Passed" if marks are 40 or above

  • "Failed" otherwise

This is where if, elif, and else work together.

if Statement

The if statement checks the first condition.

marks = 85

if marks >= 40:
    print("Passed")

If the condition is True, its block runs.

elif Statement

elif means "else if".

It lets us check another condition when the previous if condition was False.

marks = 85

if marks >= 90:
    print("Excellent")
elif marks >= 75:
    print("Good")

Since marks >= 90 is false, Python checks the elif condition.

Output:

Good

You can have multiple elif blocks:

marks = 68

if marks >= 90:
    print("Excellent")
elif marks >= 75:
    print("Good")
elif marks >= 60:
    print("Average")
elif marks >= 40:
    print("Passed")

Python checks the conditions from top to bottom and executes the first condition that is true.

else Statement

The else block runs when none of the previous conditions are true.

marks = 30

if marks >= 40:
    print("Passed")
else:
    print("Failed")

Output:

Failed

You can think of else as the fallback option.

Using if, elif, and else Together

Here's a complete example:

marks = 82

if marks >= 90:
    print("Excellent")
elif marks >= 75:
    print("Good")
elif marks >= 40:
    print("Passed")
else:
    print("Failed")

Output:

Good

The flow is:

marks >= 90?  → No
marks >= 75?  → Yes → Print "Good"

Once Python finds a true condition, it skips the remaining elif and else blocks.

Order of Conditions Matters

This is important.

Consider:

marks = 95

if marks >= 40:
    print("Passed")
elif marks >= 90:
    print("Excellent")

The output will be:

Passed

Why? Because 95 >= 40 is already true, so Python never reaches the elif.

The more specific condition should come first:

if marks >= 90:
    print("Excellent")
elif marks >= 40:
    print("Passed")

Now the output is:

Excellent

elif Without else

You don't have to use else.

temperature = 35

if temperature > 40:
    print("Very hot")
elif temperature > 30:
    print("Hot")

If none of the conditions are true, Python simply does nothing.

Nested Conditions

You can also put an if statement inside another if statement.

age = 20
has_id = True

if age >= 18:
    if has_id:
        print("Entry allowed")
    else:
        print("ID required")
else:
    print("You are too young")

Nested conditions are useful when one decision depends on another, although logical operators can sometimes make the same logic simpler.

Practical Example

Let's create a simple grade calculator:

marks = int(input("Enter your marks: "))

if marks >= 90:
    print("Grade: A")
elif marks >= 75:
    print("Grade: B")
elif marks >= 60:
    print("Grade: C")
elif marks >= 40:
    print("Grade: D")
else:
    print("Grade: F")

If the user enters 78:

Grade: B

The Basic Pattern

Whenever you need to choose between multiple possibilities, remember this pattern:

if condition1:
    # code
elif condition2:
    # code
elif condition3:
    # code
else:
    # code

So, in simple terms:

  • if → checks the first condition.

  • elif → checks additional conditions.

  • else → runs when none of the conditions are true.

Together, they allow your Python programs to make clear and flexible decisions.