Conditional Statements in Python
Python's if, elif, and else statements form the backbone of program control flow logic. They let you run different blocks of code based on specific conditions.
In this guide, you'll learn how the if-elif-else structure works. We'll cover when to use each part. You'll also discover how to avoid common mistakes and write clean, efficient code.
What Are Conditional Statements in Python
Conditional statements allow your code to run only when a certain condition is met. These statements are the foundation of program logic and help you build interactive applications.
if condition:
# code runs if the condition is True
elif another_condition:
# code runs if the first condition is False but this one is True
else:
# code runs if none of the above conditions are True
Conditional statements work with boolean expressions. These expressions return either True or False. Based on the result, the corresponding block of code executes.
Basic Syntax of the if Statement
The simplest conditional statement, if, runs a block of code only when the condition is met:
x = 10
if x > 5:
print("x is greater than 5")
In this example, x > 5 is a boolean expression. When it returns True, the body of the if block executes. Notice the colon after the condition and the indentation on the next line.
Using the else Statement
The else statement provides an alternative path for code execution:
x = 2
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")
The else block is optional. It runs only when the condition in the if statement is False. This lets you handle all possible cases.
Multiple Conditions with elif
The elif statement lets you check additional conditions:
x = 0
if x > 0:
print("Positive number")
elif x == 0:
print("Zero")
else:
print("Negative number")
elif is short for "else if." In Python, this is the official syntax for checking multiple conditions. You can use as many elif blocks as you need.
How if, elif, and else Statements Work
Python evaluates conditions sequentially from top to bottom. This process follows a clear execution logic.
First, the condition in the if block is checked. If it's True, the corresponding code runs, and all remaining blocks are skipped.
If the if condition is False, Python moves on to check the elif conditions in order. As soon as a True condition is found, its block of code executes.
If none of the if or elif conditions are met, the else block runs. The else block is optional and can be omitted.
Important: only one block of code will execute, even if multiple conditions are True at the same time.
Practical Examples
Grading System
Here's a program that determines a grade based on a score:
score = 85
if score >= 90:
print("Excellent")
elif score >= 75:
print("Good")
elif score >= 60:
print("Satisfactory")
else:
print("Needs improvement")
In this example, conditions are checked in descending order. This ensures the correct grade category is assigned.
Checking if a Number is Even or Odd
A simple way to determine if a number is even or odd:
n = 7
if n % 2 == 0:
print("Even")
else:
print("Odd")
The % operator returns the remainder of a division. If the remainder is zero, the number is even.
Determining Age Category
age = 25
if age < 18:
print("Minor")
elif age < 65:
print("Adult")
else:
print("Senior")