if statement
Updated March 16, 2026 · Keywords
keyword conditional control-flow boolean
The if keyword is Python’s primary conditional statement. It executes code only when a specified condition evaluates to True.
Syntax
if condition:
# Code runs when condition is True
You can add elif and else branches:
if condition1:
# Runs if condition1 is True
elif condition2:
# Runs if condition1 is False and condition2 is True
else:
# Runs if both conditions are False
Basic Examples
Simple Condition
x = 10
if x > 5:
print("x is greater than 5")
# x is greater than 5
With else
x = 3
if x > 5:
print("x is greater than 5")
else:
print("x is 5 or less")
# x is 5 or less
With elif
score = 75
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
elif score >= 60:
grade = "D"
else:
grade = "F"
print(f"Grade: {grade}")
# Grade: C
Conditions and Boolean Values
Truthy and Falsy Values
Python treats certain values as False (falsy):
NoneFalse- Zero:
0,0.0,0j - Empty sequences:
"",[],() - Empty dicts:
{} - Objects with
__bool__()returningFalseor__len__()returning0
Everything else is truthy:
if "hello":
print("String is truthy")
# String is truthy
if [1, 2, 3]:
print("Non-empty list is truthy")
# Non-empty list is truthy
if 42:
print("Non-zero number is truthy")
# Non-zero number is truthy
Comparison Operators
x = 5
y = 10
if x < y:
print("x is less than y")
# x is less than y
if x == 5:
print("x equals 5")
# x equals 5
if x != 0:
print("x is not zero")
# x is not zero
Logical Operators
Combine conditions with and, or, not:
age = 25
has_license = True
if age >= 18 and has_license:
print("Can drive")
# Can drive
if age < 18 or not has_license:
print("Cannot drive")
Ternary Conditional Expression
One-line conditional expression:
age = 20
status = "adult" if age >= 18 else "minor"
print(status)
# adult
Nested Conditionals
x = 10
if x > 0:
if x > 5:
print("x is positive and greater than 5")
else:
print("x is positive but 5 or less")
else:
print("x is not positive")
# x is positive and greater than 5
Common Patterns
Checking None
value = None
if value is None:
print("No value provided")
# No value provided
value = "hello"
if value is not None:
print(f"Value is: {value}")
# Value is: hello
Empty Collection Check
my_list = []
if my_list:
print("List has items")
else:
print("List is empty")
# List is empty
Multiple Conditions
user_role = "admin"
access_level = 3
if user_role == "admin" and access_level >= 2:
print("Full access granted")
# Full access granted
Best Practices
Avoid Redundant Comparisons
# Avoid
if is_valid == True:
# Prefer
if is_valid:
Use elif Instead of Nested if
# Avoid
if x > 10:
print("A")
else:
if x > 5:
print("B")
else:
print("C")
# Prefer
if x > 10:
print("A")
elif x > 5:
print("B")
else:
print("C")
Keep Conditions Simple
Extract complex conditions into descriptive variables:
# Less readable
if user.is_active and user.has_subscription and user.subscription.is_valid:
print("Can access")
# More readable
can_access = (
user.is_active
and user.has_subscription
and user.subscription.is_valid
)
if can_access:
print("Can access")
See Also
- elif keyword — additional conditional branches
- continue keyword: skip to next iteration
- for keyword — loop based on conditions
- break keyword — exit conditional blocks