# A: What are Nested Conditionals in Python?

*By Waqas Hussain (GlitchWH) Computer System Engineering student specializing in Python and Cyber Security*

### Introduction

A program often needs to make a decision based on the result of another decision. Placing an `if` inside another `if` is called a **nested conditional**. The outer condition guards the inner one.

![](https://cdn.hashnode.com/uploads/covers/6a045b1b5418a55a522a6d57/942d2a78-85de-4188-856e-2c9ac5268815.png align="center")

![](https://cdn.hashnode.com/uploads/covers/6a045b1b5418a55a522a6d57/2ad8c1f7-1aef-4bc1-86e9-5ebbde9f2eca.png align="center")

### Step 1: What a Nested Conditional Is

A nested conditional is an `if`, `elif`, or `else` block placed inside another conditional block. The inner block is part of the outer block's body. It can only run if the outer condition allows it.

### Step 2: Indentation Defines the Structure

Python has no braces. Indentation alone decides which block a line belongs to. `if owns_id:` is indented under `if person_age >= 18:`, so it is inside it. `print("Has access to Library")` is one level deeper, so it belongs to `if owns_id:`.

### Step 3: The Outer Condition Is Checked First

Python evaluates person\_`age >= 18` first. If it is `False`, the whole inner block is skipped and `owns_id` is never evaluated. Try person\_`age = 15`: the output is cant enter the Library, and the inner check never runs.

### Step 4: Which `else` Belongs to Which `if`

An `else` or `elif` pairs with the `if` at the **same indentation level**.

*   The inner `else` runs only when person\_`age >= 18` is true and `owns_id` is false.
    
*   The outer `else` runs only when person\_`age >= 18` is false.
    

### Step 5: `elif` Inside Nested Blocks

Each nested level can have its own `elif` chain:

![](https://cdn.hashnode.com/uploads/covers/6a045b1b5418a55a522a6d57/ff396328-4713-4dbf-b839-f86a8eb51374.png align="center")

![](https://cdn.hashnode.com/uploads/covers/6a045b1b5418a55a522a6d57/ec3cafae-67b6-4599-b809-efd81a60cba4.png align="center")

Inside a chain, Python stops at the first condition that is true.

### Step 6: Flattening Nested Conditionals

Deep nesting is hard to read. Two fixes:

![](https://cdn.hashnode.com/uploads/covers/6a045b1b5418a55a522a6d57/0174e05e-3d17-4369-a158-34db01485bca.png align="center")

![](https://cdn.hashnode.com/uploads/covers/6a045b1b5418a55a522a6d57/f699050e-4e65-4259-9520-fa56ad06b413.png align="center")

i will leave this to your understandings untill we start the functions

### Conclusion

A nested conditional places a decision inside another decision. Indentation defines the structure, the outer condition is evaluated first, and each `else` pairs with the `if` at its own indentation level. If nesting goes deeper than about three levels, flatten it with `and` or early returns.

**Next up:** B: *Nested Loops in Python*
