Nearby lessons
41 of 159Python - If-Elif-Else Statement
📌 What You Will Learn
- Define the if-elif-else ladder
- Explain how conditions are checked from top to bottom
- Write a grading program that runs only the first matching block
- Use the final else as a fallback block
- Apply the elif ladder in grading systems, ATM menus and login applications
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
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.
📝 Key Takeaways
- The if-elif-else ladder checks multiple conditions and runs only the first matching block
- Conditions are evaluated from top to bottom
- Once a condition becomes True, the remaining blocks are skipped
- The final else acts as a fallback block when no condition matches
- elif is short for else if
🧠 Test Your Knowledge
5 QuestionsProgress: 0 / 5