Python Nested if Statements With Example
In this lesson, I will teach you about Python Nested if Statements with example. In some situations, there is a need to use nested if statements. Now the question is what does mean by nested if statements.
So, when you have a piece of code inside another code it is called nesting. You can nest if..elif..else inside of another if..elif..else statement. There is no limit on the nesting of one code inside of the other code you can nest as many as you want. But as nesting increasing confusion also increasing so the only way to not have confusion is to use indentation to check the level of each nesting.
Syntax Of Python Nested if Statements
if test expression code 1: statement if test expression code 2: statement elif test expression code 3: statement elif test expression cdoe 4: statement else: statement else: statement
Example Of Python Nested if Statements
# Here we will test the nested if..elif..else statements. # Here we will use value 200 as an example value = 200 if value < 300: print "The value is less than 300" if value == 250: print "The value is 250" elif value == 200: print "The value is 200" elif value == 50: print "The value is 50" elif var < 50: print "Expression value is less than 50" else: print "Could not find true expression" print "Good bye!"