Nearby lessons

41 of 159

Python - 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:
    statements

How It Works

  • Conditions are checked from top to bottom.
  • Once one condition becomes true, the remaining blocks are skipped.
  • Indentation is mandatory.
  • The final else acts as a fallback block.

Example

🐍Code Cell
1marks = 85
2 
3if marks >= 90:
4 print('Grade A')
5elif marks >= 60:
6 print('Grade B')
7else:
8 print('Grade C')
Output
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.

📝 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 Questions
Progress: 0 / 5