Nested Conditionals
Nested conditionals are if/else statements inside other if/else statements. Use them when decisions depend on multiple levels of criteria. However, try to keep nesting shallow (2-3 levels max) for readability.
15 min•By Priygop Team•Updated 2026
Nested Conditionals
Nested Conditionals
# Nested if-else example
age = 25
has_ticket = True
is_vip = False
if age >= 18:
if has_ticket:
if is_vip:
print("Welcome to the VIP section!")
else:
print("Welcome! Enjoy the show.")
else:
print("You need a ticket to enter.")
else:
print("Sorry, this event is 18+.")
# Cleaner approach: combine conditions
if age >= 18 and has_ticket and is_vip:
print("VIP access granted!")
elif age >= 18 and has_ticket:
print("General admission.")
elif age >= 18:
print("Buy a ticket first.")
else:
print("Must be 18+.")Tip
Tip
Flatten nested conditionals by combining with 'and'. if age >= 18 and has_ticket: is cleaner than nesting two if statements.
Diagram
Loading diagram…
Everything in Python is an object — use type() to check
Common Mistake
Warning
Deeply nested conditionals (3+ levels) are a code smell. Refactor using combined conditions or guard clauses.
Practice Task
Note
Refactoring challenge: (1) Write a 3-level nested if. (2) Refactor it using combined 'and' conditions. (3) Compare readability.