Programming with Python

  1. Home
  2. Docs
  3. Programming with Python
  4. Conditional statements in python
  5. Nested if-else statement in python

Nested if-else statement in python

A nested if is an if that has another if in its if’s body or elif’s body or else’s body.

syntax:-

if expression1:
   statement(s)
   if expression2:
      statement(s)
   else:
      statement(s)
else:
   statement(s)

e.g.,

n = 5
if n >= 0: 
    if n == 0: 
       print("Zero") 
    else: 
       print("Positive number") 
else: 
    print("Negative number") 
Output- positive number

This is how we use nested if statement in python.

Was this article helpful to you? Yes No

How can we help?