This article explains how to use if, elif, and else statements, including nested conditions and short-hand if expressions for compact logic.
What Are Conditional Statements?
A conditional statement evaluates an expression and executes a specific block of code depending on whether the condition is True or False. Python uses the following keywords for conditional logic:if
elif (short for else if)
else
The if Statement
The if statement is used to test a single condition. If the condition evaluates to True, the code inside the if block executes.Syntax:
if condition:
statement(s)
Example:
age = 18
if age >= 18:
print("You are eligible to vote.")
Output:
You are eligible to vote.
Note: The indentation (space before statements) defines the scope of the if block in Python.
The if-else Statement
Sometimes, you want to execute one block when the condition is True and another when it is False. In such cases, use an if-else structure.Syntax:
if condition:
statement(s)
else:
statement(s)
Example:
num = 5
if num % 2 == 0:
print("Even number")
else:
print("Odd number")
Output:
Odd number
The if-elif-else Ladder
When you have multiple conditions, you can use the if-elif-else ladder. The program checks conditions from top to bottom and executes the first one that is True.Syntax:
if condition1:
statement(s)
elif condition2:
statement(s)
elif condition3:
statement(s)
else:
statement(s)
Example:
marks = 82
if marks >= 90:
print("Grade: A")
elif marks >= 75:
print("Grade: B")
elif marks >= 60:
print("Grade: C")
else:
print("Grade: D")
Output:
Grade: B
Only one block executes β the first one whose condition evaluates to True.
Nested if Statements
You can place an if statement inside another if block. This is known as a nested if. It's useful for checking multiple levels of conditions.Example:
age = 25
citizen = True
if age >= 18:
if citizen:
print("Eligible to vote.")
else:
print("Not a citizen, cannot vote.")
else:
print("Not eligible due to age.")
Output:
Eligible to vote.
Use nesting only when necessary β too many nested levels can make code harder to read.
Short-Hand if Expressions
Python allows you to write if statements in a single line when thereβs only one statement to execute.Example 1: Single-line if
x = 10
if x > 5: print("x is greater than 5")
Output:
x is greater than 5
Example 2: Short-hand if-else (Ternary Operator)
a = 10
b = 20
print("a is greater") if a > b else print("b is greater")
Output:
b is greater
This one-liner is often called the ternary expression in Python.
Logical Operators in Conditions
You can combine multiple conditions using and, or, and not inside if statements.Example:
age = 22
has_license = True
if age >= 18 and has_license:
print("You can drive.")
else:
print("You cannot drive.")
Output:
You can drive.
Summary
Conditional statements form the decision-making backbone of Python programming. With if, elif, else, and nested conditions, you can control program flow and handle multiple scenarios efficiently.| Keyword | Meaning | Example |
|---|---|---|
| if | Executes a block if condition is true | if x > 0: |
| else | Executes when condition is false | else: |
| elif | Checks another condition if previous is false | elif x == 0: |
| Nested if | if statement inside another if |
if x > 0: if y > 0: |
| Short-hand if | One-line conditional expression | print("Yes") if x > 0 else print("No") |