Nearby lessons
47 of 108Python - If-Elif-Else Statement
What Is It?
The if-elif-else ladder is used when a program needs to check multiple
conditions and run only the first matching block.
It is the right choice when a single decision has many possible outcomes.
Syntax
if condition1:
statements
elif condition2:
statements
else:
statementsHow It Works
- Conditions are checked from top to bottom.
- Once one condition becomes true, the remaining blocks are skipped.
- Indentation is mandatory.
- The final
elseacts as a fallback block.
Example
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Grade B
Because the first condition is false, Python checks the next condition and stops there.
Where It Is Used
This form is common in grading systems, ATM menus, banking applications, login systems, and other menu-driven programs.
It also helps when mapping numeric codes to labels, like day numbers or status codes.
🧠 Test Your Knowledge
3 QuestionsProgress: 0 / 3
Keep Going!Python - While Loop