Lesson 12 of 60
โฎPrev Nextโฏ

PY-101 ยท Conditions

Nested conditions

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

12of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    A nested condition is an if inside another if, so the inner check only happens when the outer one is True. It suits decisions that come in stages. When nesting gets deep, combining conditions with and/or is usually clearer.

    • Nest one if inside another
    • Read indentation levels
    • Flatten with and / or

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    Nested conditions, running
    1. has_ticket = True
    2. age = 16
    3. if has_ticket:
    4. if age >= 18:
    5. print("Enjoy the film")
    6. else:
    7. print("Sorry, this film is 18+")
    8. else:
    9. print("Please buy a ticket")
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemA cinema checks your ticket first. Only if you have one does it check your age for the 18+ film. That second question only matters after the first.
    2. The Python ideaif outer:A nested condition is an if inside another if, so the inner check only happens when the outer one is True.
    3. In real lifeCheckout code checks the cart is not empty, then whether the card is valid, then whether there is stock, each step inside the last.

    Start with a question

    A cinema checks your ticket first. Only if you have one does it check your age for the 18+ film. That second question only matters after the first.

    Real rules come in layers: is the user signed in, and if so, are they an admin? Nesting expresses that order.

    Explain it like I'm 10

    First the guard asks "do you have a ticket?". Only if you say yes does he ask "how old are you?". The second question sits inside the first.

    The technical explanation

    Each level of nesting adds four more spaces. The inner if only runs when the outer condition is True.

    if a: if b: does the same as if a and b: when there is no else for the outer check. The flat version is easier to read.

    Keep nesting when the inner branch needs its own else, such as a different message for "has ticket but too young".

    Syntax

    if outer:
        if inner:
            ...
        else:
            ...

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    Checkout code checks the cart is not empty, then whether the card is valid, then whether there is stock, each step inside the last.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. When does an inner if run?
    2. 2. Which is the same as `if a:` then `if b:` (no else)?
    3. 3. How many spaces does each nesting level usually add?
    4. 4. x = 3. if x > 0: if x > 5: print("A") else: print("B"). What prints?
    5. 5. Why avoid very deep nesting?

    Questions people ask

    How deep can if statements be nested in Python?

    There is no practical limit, but more than two or three levels is hard to read. Combine conditions with and/or, or move parts into functions.

    Is nested if slower than and?

    No meaningful difference. Choose whichever is clearer to read.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner How deep can if statements be nested in Python?

    There is no practical limit, but more than two or three levels is hard to read. Combine conditions with and/or, or move parts into functions.

    beginner Is nested if slower than and?

    No meaningful difference. Choose whichever is clearer to read.

    intermediate When does an inner if run?

    Only when the outer condition is True. It lives inside the outer block.

    intermediate Which is the same as `if a:` then `if b:` (no else)?

    if a and b:. Both must be True for the inner block to run.

    Quick revision

    Remember

    • Nest one if inside another
    • Read indentation levels
    • Flatten with and / or
    • More than two levels deep is a warning sign. Try returning early, or join the checks with and so the code reads like a sentence.

    Syntax

    if outer:
        if inner:
            ...
        else:
            ...

    Official documentation