Chapter 19 of 20

Nested if Statements in Python

Sometimes one decision depends on another decision.

For example, before allowing someone to enter a building, we might first check their age and then check whether they have a valid ID.

For situations like this, Python allows us to put an if statement inside another if statement. This is called a nested if statement.

What is a Nested if Statement?

A nested if statement is an if statement placed inside another if statement.

Here's the basic structure:

if condition1:
    if condition2:
        # code

The inner if is checked only when the outer if condition is True.

Simple Example

age = 20

if age >= 18:
    if age <= 60:
        print("You are eligible")

First, Python checks:

age >= 18 → True

Then it checks the inner condition:

age <= 60 → True

So the output is:

You are eligible

Nested if with else

We can also use else inside a nested condition.

age = 20
has_id = False

if age >= 18:
    if has_id:
        print("Entry allowed")
    else:
        print("Please show your ID")
else:
    print("You are too young")

Since the person is 18 or older but doesn't have an ID, the output is:

Please show your ID

Multiple Levels of Nesting

You can technically nest several levels of if statements:

age = 25
has_id = True
has_ticket = True

if age >= 18:
    if has_id:
        if has_ticket:
            print("Entry allowed")

All three conditions must be true for the message to appear.

However, deeply nested code can become difficult to read. In many cases, logical operators can make it cleaner.

For example, the previous code could be written as:

if age >= 18 and has_id and has_ticket:
    print("Entry allowed")

This is often easier to understand.

Nested if with User Input

Nested conditions can also be used with user input.

age = int(input("Enter your age: "))

if age >= 18:
    has_license = input("Do you have a driving license? ")

    if has_license == "yes":
        print("You can drive")
    else:
        print("You need a driving license")
else:
    print("You are too young to drive")

Here, the program asks about the driving license only if the user is 18 or older.

Indentation Matters

Indentation is extremely important with nested if statements.

if age >= 18:
    if has_id:
        print("Entry allowed")

Each level gets additional indentation:

if age >= 18:
    if has_id:
        print(...)

The indentation tells Python which if each statement belongs to.

When Should You Use Nested if?

Nested if statements are useful when:

  • One condition depends on another.

  • You need to perform a second check only after the first succeeds.

  • The logic is easier to understand as separate steps.

For example:

if user_is_logged_in:
    if is_admin:
        print("Admin Dashboard")

The admin check only makes sense if the user is logged in.

Nested if vs Logical Operators

Sometimes you can use either approach.

Nested if

if age >= 18:
    if has_id:
        print("Entry allowed")

Using and

if age >= 18 and has_id:
    print("Entry allowed")

Both can work, but the second version is simpler when you're just checking multiple independent conditions.

So, use nested if when the second decision naturally depends on the first. Otherwise, combining conditions with logical operators can often keep your code cleaner.