Nested If

Python · Control Flow

Theory

A nested if is an if statement placed inside the block of another if. The inner condition is only checked when the outer condition is already true. In the example, Python first tests salary > 80000; only if that passes does it look at department == 'IT', and 'Senior IT' is printed only when both hold. If the outer test fails, the inner if is skipped entirely, never even evaluated. Each level of nesting adds one more level of indentation.

Nesting is useful when the inner check only makes sense after the outer one, or when each level has its own else branch. But two conditions that simply must both be true are usually clearer combined: if salary > 80000 and department == 'IT':. Deeply nested code (three or more levels) becomes hard to read; flatten it with and, elif, or early exits such as return or continue when a condition fails. Remember that an else belongs to the if at the same indentation level.

Why we use it

Some decisions are hierarchical: first check if a user is logged in, then whether they are an admin. Nesting expresses 'only ask the second question if the first answer was yes' and lets each level have its own else.

When to use it

Use a nested if when the inner check depends on the outer one, or each level needs separate handling. When you only need 'both conditions true', combine them with and. Avoid nesting deeper than two or three levels.

How it works

  1. Reach the outer if
  2. Is salary > 80000? Yes: Enter the outer block. No: Skip the whole block, inner if included.
  3. Is department == 'IT'? Yes: Run the inner block: print('Senior IT'). No: Skip the inner block.
  4. Leave the inner block, then the outer block
  5. Continue with the code after the outer if

Syntax

if condition1:
    if condition2:
        ...

Example

salary = 85000
department = "IT"
if salary > 80000:
    if department == "IT":
        print("Senior IT")

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic