Python Tutorial

Introduction

Edit Template
  • Home
  • /
  • Python elif Statement

Python Tutorial

Introduction

Edit Template
  • 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 first elif.

  • If that’s also False, it checks the next elif, 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.

Scroll to Top