In Python, you can place one if
statement inside another. This is known as a nested condition. It allows you to check multiple conditions in a structured way and control the flow of your program based on multiple levels of logic.
Syntax
if condition1:
if condition2:
# block runs only if both conditions are True
else:
# condition1 is True, but condition2 is False
else:
# condition1 is False
Example 1: Simple Nested if
age = 20
has_id = True
if age >= 18:
if has_id:
print("Entry allowed")
Both conditions must be true for the message to print.
Example 2: Nested if-else
password = "admin123"
logged_in = True
if logged_in:
if password == "admin123":
print("Access granted")
else:
print("Wrong password")
else:
print("Please log in")
Only logged-in users proceed to password check.
Example 3: Grading System
marks = 87
if marks >= 50:
if marks >= 85:
print("Grade: A")
else:
print("Grade: B")
else:
print("Fail")
Nested conditions help in refining categories.
Example 4: Checking Discount Eligibility
membership = "Gold"
purchase_amount = 6000
if membership == "Gold":
if purchase_amount > 5000:
print("20% discount applied")
else:
print("10% discount applied")
else:
print("No discount")
Discounts depend on both membership and amount.
Example 5: Using elif Inside Nested Block
day = "Sunday"
is_holiday = True
if is_holiday:
if day == "Sunday":
print("Relax! It's Sunday")
elif day == "Saturday":
print("Weekend vibe")
else:
print("Midweek break")
else:
print("Workday")
Multiple nested options using elif
.
Example 6: Input Validation with Nested Conditions
username = "admin"
password = "secret"
if username:
if password:
print("Logged in")
else:
print("Password required")
else:
print("Username required")
Checks for both inputs in order.
Example 7: Nested in Loops (Brief Preview)
for x in [10, 20, 30]:
if x > 15:
if x == 30:
print("Thirty")
else:
print("Greater than 15")
Nested conditions often appear inside loops.
Example 8: Nested with and/or Logic
user_role = "admin"
logged_in = True
if logged_in:
if user_role == "admin" or user_role == "superuser":
print("Admin panel access")
Combines logical operators inside nested blocks.
Example 9: Banking Scenario
balance = 1000
withdraw = 500
if withdraw > 0:
if balance >= withdraw:
print("Withdrawal successful")
else:
print("Insufficient funds")
else:
print("Invalid amount")
Nested logic for secure transaction processing.
Example 10: Deep Nesting (Not Recommended)
x = 5
if x > 0:
if x < 10:
if x == 5:
print("Value is 5")
Deep nesting works but may reduce readability. Consider simplifying.
Tips for Using Nested Conditions
- Use nested
if
when one condition depends on another. - Keep nesting shallow when possible to improve readability.
- Use logical operators like
and
,or
when nesting becomes too deep. - Indent blocks clearly — Python relies on indentation for structure.
Nested conditions help manage multiple layers of logic and are especially useful when decisions depend on other decisions. Use them wisely for readable and effective Python code.