Nesting means writing one conditional statement inside another.

example

age = 20
has_id = True
 
if age >= 18:
    if has_id:
        print("You can vote")
    else:
        print("You need an ID")
else:
    print("You are underage")

output

You can vote

Syntax:

if condition1:
    if condition2:
        print(" ")

Important

  • Inner if runs only if outer if is True
  • Use proper indentation
  • Avoid too much nesting (makes code hard to read)

<< Python Conditional Statements | Python Match Case >>