Nearby lessons

47 of 108

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

🐍
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 Questions

Progress: 0 / 3
Keep Going!Python - While Loop