Python Nested if Statements With Example

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.

Recommended:  Structuring In Python Programming Language

Syntax Of Python Nested if Statements

if test expression  code 1:
   if test expression code 2:
   elif test expression code 3:
   elif test expression cdoe 4:

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"
   print "Could not find true expression"

print "Good bye!"


Recommended:  For Loop In Python Programming Language

Jack Sparrow

Hay I am Admin Of This Website. I am very passionate about Computers and technology. This website is the result of my passion. I love to eliminate the barriers comes in the for seeking education. This website is the free source of learning about new technology and up to date knowledge.

Leave a Reply

Close Menu