Lesson 20 of 70 – Nested If Statement
28%

Python Nested If Statement

A Nested If statement means using one if statement inside another if statement. It is useful when we need to check multiple conditions step by step.

Note: A nested if statement is written inside the block of another if statement.
What is Nested If?

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

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

if condition1:
    if condition2:
        statement
Basic Example
age = 20

if age >= 18:
    if age <= 60:
        print("You are eligible")
Output:
You are eligible
How Nested If Works

Python checks the conditions in sequence:

  1. First, the outer if condition is checked.
  2. If it is True, Python enters the block.
  3. Then the inner if condition is checked.
  4. If the inner condition is True, its statement executes.
Nested If with Marks
marks = 75

if marks >= 40:
    if marks >= 60:
        print("First Division")
Output:
First Division
Nested If with Age
age = 25

if age >= 18:
    if age < 30:
        print("You are a young adult")
Output:
You are a young adult
Nested If with Two Conditions
number = 10

if number > 0:
    if number % 2 == 0:
        print("Positive even number")
Output:
Positive even number
Nested If with User Input
age = int(input("Enter your age: "))

if age >= 18:
    if age >= 21:
        print("You can enter")

If the user enters 25:

Output:
You can enter
Nested If with Positive Number
number = 15

if number > 0:
    if number > 10:
        print("Number is greater than 10")
Output:
Number is greater than 10
Nested If with Login
username = "admin"
password = "1234"

if username == "admin":
    if password == "1234":
        print("Login successful")
Output:
Login successful
Nested If with Three Conditions
age = 25

if age >= 18:
    if age <= 60:
        if age >= 21:
            print("Condition satisfied")
Output:
Condition satisfied
Nested If with Else
age = 16

if age >= 18:

    if age >= 21:
        print("Eligible")
    else:
        print("Below 21")

else:
    print("Below 18")
Output:
Below 18
Nested If with Even and Odd
number = 8

if number > 0:

    if number % 2 == 0:
        print("Positive even number")
    else:
        print("Positive odd number")
Output:
Positive even number
Nested If with Marks
marks = 85

if marks >= 40:

    if marks >= 80:
        print("Excellent")
    else:
        print("Pass")

else:
    print("Fail")
Output:
Excellent
Nested If for Voting
age = 20

if age >= 18:

    if age <= 100:
        print("You can vote")
Output:
You can vote
Nested If with String
name = "Rahul"

if name == "Rahul":

    if len(name) > 3:
        print("Valid name")
Output:
Valid name
Nested If with AND Logic

Sometimes nested conditions can also be written using the and operator.

age = 25

if age >= 18:
    if age <= 60:
        print("Working age")

The same logic can be written as:

if age >= 18 and age <= 60:
    print("Working age")
Both approaches can produce the same result.
Nested If vs Multiple Conditions

Nested If:

if age >= 18:
    if age <= 60:
        print("Eligible")

Using and:

if age >= 18 and age <= 60:
    print("Eligible")

Nested if is useful when the second condition should be checked only after the first condition succeeds.

Nested If with Positive and Divisible Number
number = 20

if number > 0:

    if number % 5 == 0:
        print("Positive and divisible by 5")
Output:
Positive and divisible by 5
Important: Indentation in Nested If

Indentation is very important in Python. The inner if statement must be properly indented.

if age >= 18:
    if age >= 21:
        print("Eligible")

Here, the second if belongs to the first if.

Incorrect:
if age >= 18:
if age >= 21:
print("Eligible")
This will cause an indentation error.
Nested If in a Real-Life Example

Suppose a student wants to take an exam. First, we check whether the student has passed the minimum attendance. Then we check whether the student has paid the exam fee.

attendance = 80
fee_paid = True

if attendance >= 75:

    if fee_paid:
        print("Student can appear in exam")

else:
    print("Attendance is too low")
Output:
Student can appear in exam
Nested If with Multiple Levels

Python allows an if statement to be nested inside another if statement and another if statement.

number = 20

if number > 0:

    if number < 100:

        if number % 2 == 0:
            print("Positive even number below 100")
Output:
Positive even number below 100
Key Points
  • Nested if means an if statement inside another if statement.
  • The inner if runs only when the outer condition is True.
  • Proper indentation is required.
  • Nested if can contain an else statement.
  • Multiple levels of nested if are possible.
  • Nested conditions can sometimes be replaced by logical operators such as and.
  • Nested if is useful for checking conditions step by step.

🧠 Quick Quiz

Question: What is a nested if statement in Python?