0
0
Pythonprogramming~5 mins

Elif ladder execution in Python

Choose your learning style9 modes available
Introduction

An elif ladder helps your program choose between many options by checking conditions one by one.

When you want to pick one action from many choices, like choosing a menu item.
When you need to check different ranges of numbers, like grading scores.
When you want to respond differently based on user input, like a simple quiz.
When you want to handle multiple conditions without repeating code.
When you want your program to stop checking once it finds the first true condition.
Syntax
Python
if condition1:
    # do something
elif condition2:
    # do something else
elif condition3:
    # do another thing
else:
    # do if none above matched

The elif means "else if" and lets you check more conditions after the first if.

The else part runs only if none of the above conditions are true.

Examples
This checks age and prints the right group.
Python
age = 20
if age < 13:
    print("Child")
elif age < 20:
    print("Teenager")
else:
    print("Adult")
This assigns a grade based on score ranges.
Python
score = 85
if score >= 90:
    print("A grade")
elif score >= 80:
    print("B grade")
elif score >= 70:
    print("C grade")
else:
    print("Fail")
Sample Program

This program checks the temperature and prints a message that matches the weather.

Python
temperature = 30
if temperature > 35:
    print("It's very hot")
elif temperature > 25:
    print("It's warm")
elif temperature > 15:
    print("It's cool")
else:
    print("It's cold")
OutputSuccess
Important Notes

Only the first true condition runs; the rest are skipped.

Order matters: put the most specific conditions first.

You can have as many elif parts as you want.

Summary

An elif ladder helps pick one option from many.

It checks conditions in order and stops at the first true one.

Use else to handle all other cases.