Computer >> Computer tutorials >  >> Programming >> Python

How to use nested if statement in Python?


In programming the term nesting is used when conditional statements appear one inside other. In Python, new if statement can be nested in if as well as else block. A you must be knowing, each if block has increasing level of indentation. Hence in case of nested if condition, indent level of inner block will increase from outer block. A general syntax of nested if can be as below −

if expr1==True:
    if expr2==True:
        #this block will be executed when expr1 and expr2 are true
    else:
        #this block if expr is true but expr2 is false
else:
     if expr3==True:
        #if expr1 is false and expr3 is true
    else:
         #when expr1 and expr3 are false