Nearby lessons

46 of 108

Python - Nested if Statement

What Is Nested if?

A nested if statement means writing one if inside another if.

The inner condition runs only after the outer condition becomes true.

Syntax

if condition1:
    if condition2:
        statements

Basic Example

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Eligible for Voting

This pattern is useful when a second check depends on the first one passing.

Why Use It?

Nested conditions are useful when one condition depends on another condition. The inner block runs only when the outer condition is true.

Login validation and eligibility checks are common real-world examples.

Multiple Levels

Python supports multiple nested levels, so we can check more than two conditions when needed.

Deep nesting should be used carefully so the code stays readable.

Key Point

If the outer if becomes false, the inner condition is never checked.

🧠 Test Your Knowledge

3 Questions

Progress: 0 / 3
Keep Going!Python - Elif Ladder