bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/Python/Foundations Practice
Python•Foundations Practice

Python Nested If

Nested If Statements

You can have if statements inside if statements. This is called nested if statements.

Example x = 41 if x > 10:

print("Above ten,")

if x > 20:

print("and also above 20!")
else:
print("but not above 20.")
In this example, the inner if statement only runs if the outer condition (

Formula

x > 10

) is true.

How Nested If Works

Formula

Each level of nesting creates a deeper level of decision - making. The code evaluates from the outermost condition inward.

Example

Checking multiple conditions with nesting:

Formula

age = 25 has_license = True if age >= 18:
if has_license:
print("You can drive")
else:
print("You need a license")
else:
print("You are too young to drive")

Multiple Levels of Nesting

You can nest as many levels deep as needed, but keep in mind that too many levels can make code harder to read.

Example

Three levels of nesting:

Formula

score = 85 attendance = 90 submitted = True if score >= 60:
if attendance >= 80:
if submitted:
print("Pass with good standing")
else:
print("Pass but missing assignment")
else:
print("Pass but low attendance")
else:
print("Fail")

Nested If vs Logical Operators

Sometimes nested if statements can be simplified using logical operators like and. The choice depends on your logic.

Example

This nested if:

Formula

temperature = 25 is_sunny = True if temperature > 20:
if is_sunny:
print("Perfect beach weather!")

Example

Could also be written with and

Formula

temperature = 25 is_sunny = True if temperature > 20 and is_sunny:
print("Perfect beach weather!")

Both approaches produce the same result. Use nested if statements when the inner logic is complex or depends on the outer condition. Use and when both conditions are simple and equally important.

More Examples

Example

Login validation with nested checks:

username = "Emil" password = "python123"

Formula

is_active = True if username:
if password:
if is_active:
print("Login successful")
else:
print("Account is not active")
else:
print("Password required")
else:
print("Username required")

Example

Grade calculation with nested logic:

Formula

score = 92 extra_credit = 5 if score >= 90:
if extra_credit > 0:
print("A+ grade")
else:
print("A grade")
elif score >= 80:
print("B grade")
else:
print("C grade or below")

Previous

Python Shorthand If

Next

Python Pass Statement