Python Nested Conditions

In the last lesson you learned the elif Statement, which lets you check several options in a row. That handles β€œpick one of many”. But sometimes a check only makes sense after another check already passed. For that you put one if inside another if. That is a nested condition, and it is what this lesson is about.

πŸ€” Why do we need nested conditions?

Here is the pain. Imagine a website. You want to show the admin panel. But it only makes sense to even ask β€œis this person an admin?” once you already know they are logged in. Asking about admin rights for someone who is not logged in is pointless.

So you have a check that depends on an earlier check. A nested condition solves this. You run the second check only when the first one passes.

  • First decide the big thing. Is the user logged in?
  • Only then ask the smaller thing inside it. Are they an admin?
  • The inner question never even gets asked if the outer one is false.

So nesting lets one decision sit inside another. The inside check waits for the outside check to say yes first.

🧱 What is a nested condition?

A nested condition is simply an if placed inside the body of another if. The outer if runs first. If it is true, Python goes inside and runs the inner if. If the outer one is false, Python skips the whole block and never looks at the inner check.

Think of it like a building with a locked front door. The front door is the outer if. Only after you get through it can you reach the room inside. That inner room is the inner if. No front door, no room.

Here is the shape of it.

if outer_condition:
if inner_condition:
# this runs only when BOTH are true
do_something()

Read it top to bottom. Python checks outer_condition first. If that is true, it steps inside and checks inner_condition. The line do_something() only runs when both checks are true.

πŸ” A real example: the admin panel

Let’s build the login example from earlier. We have two facts about a person. Are they logged in, and are they an admin? We show the admin panel only when both are true.

logged_in = True
is_admin = True
if logged_in:
print("Welcome back!")
if is_admin:
print("Showing the admin panel.")

Let’s walk through it line by line.

  • logged_in = True and is_admin = True set up our two facts.
  • The outer if logged_in: checks the first fact. It is true, so Python goes inside.
  • print("Welcome back!") runs, because the user is logged in.
  • The inner if is_admin: is the nested check. It only runs because the outer one passed. It is true, so the admin line prints.

Output

Welcome back!
Showing the admin panel.

Now change one fact. Say the person is logged in but is not an admin.

logged_in = True
is_admin = False
if logged_in:
print("Welcome back!")
if is_admin:
print("Showing the admin panel.")

Python still enters the outer block, so the welcome line prints. But the inner if is_admin: is now false, so the admin line is skipped.

Output

Welcome back!

And if logged_in is False? Then Python never enters the outer block at all. Nothing prints. The inner check never even gets a chance to run.

πŸ“ Indentation shows the nesting

This is the part to really understand. In Python, indentation is the spacing at the start of a line. It is not just for looks. Indentation is how Python knows which lines belong inside which block.

Each step deeper to the right means β€œI am inside the thing above me”. Look at the levels here.

if logged_in: # level 0: the outer if
print("Welcome back!") # level 1: inside the outer if
if is_admin: # level 1: still inside the outer if
print("Admin panel") # level 2: inside the inner if

Reading the levels:

  • Level 0 sits at the far left. That is the outer if.
  • Level 1 is one step in. These lines run when the outer if is true.
  • Level 2 is two steps in. This line runs only when both the outer and inner if are true.

So the deeper a line sits, the more conditions had to be true to reach it. The standard step in Python is four spaces per level. Keep it the same the whole way down.

Mixing tabs and spaces breaks it

Pick spaces and stick with spaces. If you mix tab characters and spaces in the same block, Python raises an IndentationError even when the code looks lined up on your screen. Most editors can be set to insert four spaces when you press Tab, which avoids the whole problem.

🀝 When and is cleaner than nesting

Here is something worth knowing. Look at our admin example again. Both checks just need to be true, and there is no separate code in between. When that is the case, you do not need nesting at all. You can join the two checks with and.

The and keyword is true only when the conditions on both sides are true. So this flat version does the same job.

# βœ… Good: one flat check with and
if logged_in and is_admin:
print("Showing the admin panel.")

Compare that with the nested version doing the exact same thing.

# ❌ Avoid: nesting when you have nothing in between
if logged_in:
if is_admin:
print("Showing the admin panel.")

Both print the same line. But the and version is shorter and easier to read. So here is a simple guideline. If the only thing the outer if does is hold another if, flatten it with and.

When should you actually keep the nesting? When the outer block has its own separate work to do. In our first example the outer block printed "Welcome back!" for every logged-in user, admin or not. That extra line belongs to the outer check, so nesting is the right choice there.

⚠️ Common Mistakes

A few things commonly cause mistakes with nested conditions. Watch for these.

  • Forgetting to indent the inner block. The inner if and its body must sit further right than the outer if. Line them up wrong and Python complains or runs the wrong code.
# ❌ Avoid: inner block not indented inside the outer if
if logged_in:
if is_admin:
print("Admin panel")
# βœ… Good: inner if is indented inside the outer if
if logged_in:
if is_admin:
print("Admin panel")
  • Nesting too deep. Three, four, five levels of if inside if get very hard to follow. If you find yourself going that deep, it is a sign to flatten with and or split the logic into smaller pieces.
  • Using nesting when and would do. If both checks just need to be true and nothing sits between them, join them with and instead.
  • Inconsistent indentation. Use the same four-space step at every level. Mixing two spaces here and four spaces there confuses both you and Python.

βœ… Best Practices

  • Keep nesting shallow. One or two levels is fine. Deeper than that and the code gets hard to read.
  • When two checks just need to both be true, prefer if a and b: over an if inside an if.
  • Keep nesting when the outer block has its own work to do, separate from the inner check.
  • Use a steady four-space indent at every level so the structure is easy to see at a glance.

🧩 What You’ve Learned

You can now make one decision depend on another. Here is the short version.

  • βœ… A nested condition is an if inside another if, used when the inner check only matters after the outer one passes.
  • βœ… The inner block runs only when both the outer and inner conditions are true.
  • βœ… Indentation levels show the nesting. Deeper to the right means more conditions had to be true to reach that line.
  • βœ… When two checks just need to both be true, if a and b: is cleaner than nesting.
  • βœ… Deep nesting is hard to read, so keep it shallow and flatten with and where you can.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    When does the inner block of a nested if run?

    Why: Python checks the outer `if` first. It steps into the inner `if` only if the outer one is true, so the inner block needs both to be true.

  2. 2

    In Python, what shows that one if is nested inside another?

    Why: Python uses indentation to show structure. A block that sits further to the right is inside the block above it.

  3. 3

    You only want code to run when both logged_in and is_admin are true, with nothing in between. What is cleaner?

    Why: When both checks just need to be true and nothing sits between them, joining them with `and` is shorter and easier to read than nesting.

  4. 4

    If the outer condition is False, what happens to the inner if?

    Why: If the outer `if` is false, Python skips its whole block, so the inner `if` is never even looked at.

πŸš€ What’s Next?

You can branch one decision off another now. Next, let’s look at a cleaner way to compare one value against many options.

Match Case Statement

Share & Connect