Python elif Statements :

The elif Statement :

The elif keyword is pythons way of saying "if the previous conditions were not true, then try this condition".

Syntax :

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


Example :

  1. a = 786
  2. b = 786
  3. if(a < b) : print("a greater than b")
  4. elif (a > b) : print("b greater than a [elif statement]")
  5. elif (a==b) : print("a and b are equal [elif statement]")
  6. else : print("a and b are not equal")
  7. input()

Output :

python elif statements

Nested if-elif-else Statement :

C++ nested if Statements | C# nested if Statements - There may be a situation when you want to check for another condition after a condition resolves to true. In such a situation, you can use the nested construct.

Example :

  1. a = 200
  2. b = 20
  3. c = 200
  4. if (a == c):
  5. if (a > 200):
  6. print("a is small [nested - 1]")
  7. elif (a == 200):
  8. print("a is equal [nested - 1]")
  9. if( a != b ) :
  10. print("a is not equal to b [nested - 2]")
  11. elif(a == b) :
  12. print("a is equal to b [[nested - 2]]")
  13. else :
  14. print("another expression [nested - 2]")
  15. else:
  16. print("a is not equal [nested - 1]")
  17. elif (a == b):
  18. print("a and b are equal")
  19. else:
  20. print("a is not equal")
  21. input()

Output :

python nested if-elif-else statements

Next Update

Loading...

We have two YouTube channels their name is CodeX or NightHawk. CodeX is for programming related videos and NightHawk is for online game streaming so please supporting us and Subscribe to our channels