Back to Python Notes
Topic #75

Nested If

Nested ifs place conditions inside conditions for dependent logic, like checking a login before a role. Keep nesting shallow to stay readable.


Nested If Statements

You can have if statements inside if statements. This is called nested if statements.

Example

x = 41

if x > 10:
  print("Above ten,")
  if x > 20:
    print("and also above 20!")
  else:
    print("but not above 20.")

In this example, the inner if statement only runs if the outer condition (x > 10) is true.


How Nested If Works

Each level of nesting creates a deeper level of decision-making. The code evaluates from the outermost condition inward.

Example

age = 25
has_license = True

if age >= 18:
  if has_license:
    print("You can drive")
  else:
    print("You need a license")
else:
  print("You are too young to drive")

Multiple Levels of Nesting

You can nest as many levels deep as needed, but keep in mind that too many levels can make code harder to read.

Example

score = 85
attendance = 90
submitted = True

if score >= 60:
  if attendance >= 80:
    if submitted:
      print("Pass with good standing")
    else:
      print("Pass but missing assignment")
  else:
    print("Pass but low attendance")
else:
  print("Fail")

Nested If vs Logical Operators

Sometimes nested if statements can be simplified using logical operators like and. The choice depends on your logic.

Example

temperature = 25
is_sunny = True

if temperature > 20:
  if is_sunny:
    print("Perfect beach weather!")

Example

temperature = 25
is_sunny = True

if temperature > 20 and is_sunny:
  print("Perfect beach weather!")

Both approaches produce the same result. Use nested if statements when the inner logic is complex or depends on the outer condition. Use and when both conditions are simple and equally important.


More Examples

Example

username = "Emil"
password = "python123"
is_active = True

if username:
  if password:
    if is_active:
      print("Login successful")
    else:
      print("Account is not active")
  else:
    print("Password required")
else:
  print("Username required")

Example

score = 92
extra_credit = 5

if score >= 90:
  if extra_credit > 0:
    print("A+ grade")
  else:
    print("A grade")
elif score >= 80:
  print("B grade")
else:
  print("C grade or below")

Want to go beyond the notes?

Join Coding Now Tech Institute's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Nested If – FAQs

Quick answers about learning Nested If in Python.

This free note from Coding Now Tech Institute explains Nested If in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on Coding Now Tech Institute, including Nested If, is 100% free with no signup required.
With focused practice, most students grasp Nested If in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now