- Home
- /
- Python elif Statement
Python Tutorial
Introduction
Python Data Types & Oper...
Python Strings
Python Lists
Python Tuples
Python Sets
Python Dictionaries
Conditional Statements
Python Loops
Python Functions
Python Modules
Python OOPS
Advanced Topics
File Handling
- Home
- /
- Python elif Statement
elif Statement
There are times when you want to check several different conditions, not just two like in if-else
. That’s where the elif
(short for else if) statement comes in.
With elif
, Python checks conditions one by one from top to bottom. As soon as it finds one that’s True
, it runs that block and skips the rest.
How it Works
Python checks the
if
condition.If it’s
False
, it moves to the firstelif
.If that’s also
False
, it checks the nextelif
, and so on.If none of them match, the
else
block runs (if it’s present).
Example:
Let’s categorize a number:
number = 0
if number < 0:
print("This is a negative number.")
elif number == 0:
print("This is exactly zero.")
else:
print("This is a positive number.")
Output:
This is exactly zero.
Why Use elif?
When you have three or more conditions to check, using multiple elif
blocks helps avoid messy nested if
statements.
It makes your code cleaner and more readable.