Nested Conditionals
What you'll learn: How to place if statements inside other if statements to make multi-level decisions in your code.
The Core Idea
A nested conditional is simply an if, elif, or else block that contains another if statement inside it. This lets you check a second (or third, or fourth...) condition only if a previous condition was true or false.
Think of it like a security checkpoint at an airport: first they check if you have a ticket (if), and only then do they check if your ID matches the ticket (if inside the first if). The second check doesn't even happen unless you pass the first.
Why Nest Conditionals?
Sometimes a single if/elif/else chain isn't enough. You need to make a decision, then within that path, make another decision.
Plain-English example:
if it's a weekday:
if it's before 9 AM:
go to work
else:
you're late!
else:
enjoy your weekend
Notice the second if (checking the time) only matters if the first condition (weekday) is true.
Structure
if outer_condition:
if inner_condition:
# code runs only if BOTH are true
else:
# outer true, inner false
else:
# outer false
You can nest as deeply as needed, but too many levels can make code hard to read—use good indentation and comments!
Key Takeaway:
Nested conditionals let you make layered decisions by placing one if statement inside another, executing inner checks only when the outer condition is met.