Elif Statement

3 min read ·

The elif statement stands for “else if”. It is used when you want to check multiple conditions one by one.
Python checks conditions from top to bottom:
  • First if
  • Then elif
  • Finally else (if present)
As soon as one condition becomes True, the remaining conditions are skipped.

Syntax of elif

  • elif must come after if
  • You can use multiple elif
  • Colon : and indentation are mandatory
Note

You cannot use elif without an if.


Basic Example of elif

  • If first condition fails, Python checks elif
  • If elif is true, its block executes

if – elif – else Flow

  • Only one block executes
  • Python stops checking after first match

Multiple elif Conditions


elif with Comparison Operators


elif with Logical Operators


elif with User Input


elif vs Multiple if

Using Multiple if (Not Recommended)

Both conditions execute.

Using elif (Recommended)

Only one condition executes.
Pro Tip

Use elif when conditions are mutually exclusive.


Nested elif


Real World Example – Electricity Bill

Real World Scenario

Electricity bill calculation based on units consumed


Common Errors with elif

elif Without if

Wrong Order ❌

Higher condition should come first.
Stop

Wrong order can give logical errors even if syntax is correct.


Exercise

Practice Task
  1. Create a program to check grades using elif
  2. Check if a number is single-digit, double-digit, or more
  3. Build a simple traffic signal system using elif