PY-101 ยท Conditions
Nested conditions
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก 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.
has_ticket = Trueage = 16if has_ticket:if age >= 18:print("Enjoy the film")else:print("Sorry, this film is 18+")else:print("Please buy a ticket")
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
Sorry, this film is 18+ ๐ฏ Ticket price: children under 12 pay 100. Everyone else pays 200, but members pay 150. Use age = 30 and member = True, and print the price.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
if age < 12: price = 100, else: then inside, check member.
- 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.
- The Python idea
if outer:A nested condition is an if inside another if, so the inner check only happens when the outer one is True. - 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:
... 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.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: match statements โ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
andso the code reads like a sentence.
Syntax
if outer:
if inner:
...
else:
... Official documentation
- Boolean operations (docs.python.org)