Conditional Statements In Python

Introduction

 
In this article, I will explain conditional statements in Python. Conditional statements are also called decision-making statements. We use those statements while we want to execute a block of code when the given condition is true or false.
 
Type of condition statement in Python:
  • If statement.
  • If Else statement.
  • Elif statement.
  • Nested if statement.
  • Nested if else statement.

If statement

 
If statement is most usually used as a conditional statement. 
 
Syntax
  1. if(condition):    
  2. {    
  3. #if statement  
  4. }   
Example
 
Program for if statement,
  1. a = 10  
  2. b = 20  
  3. if a<b:  
  4.   print("a is less than b")  
Output
 
Conditional Statements In Python
 

If else statement

 
If else is a conditional statement. The statement itself says that if a given condition is true or false. True means executing the “if” statement to the output. False means executing the “else” statement to the output.
 
Syntax
  1. if(condition):    
  2. {    
  3.    # if statement  
  4. }    
  5. else:    
  6. {    
  7.    # else statement  
  8. }    
Example
 
Program for if else statement,
  1. a = 10  
  2. b = 20  
  3. if a==b:  
  4.   print("a and b are equal")  
  5. else:  
  6.   print("a and b are not equal")  
Output
 
Conditional Statements In Python
 

Elif statement

 
Elif is a shortcut of else if condition statements. In Python one or more conditions are used in the elif statement.
 
Syntax
  1. if(condition):  
  2. {  
  3.    # if statement  
  4. }  
  5. elif(condition):  
  6. {  
  7.    # elif statement  
  8. }  
  9. else:  
  10. {  
  11.    # else statement  
  12. }  
Example
 
Program for elif statement
  1. a = 10  
  2. b = 10  
  3. if a < b:  
  4.   print("a is greater than b")  
  5. elif a == b:  
  6.   print("a and b are equal")  
  7. else:  
  8.   print("b is greater than a")  
Output
 
Conditional Statements In Python 
 

Nested if statement

 
In python is using "if" statements inside other if statements it is called nested if statement.
 
Syntax
  1. if(condition):  
  2. {  
  3.      if(condition):  
  4.      {  
  5.         # if statement  
  6.      }  
  7. }  
Example
 
Program for nested if statement,
  1. a= 1001  
  2. if a> 100:  
  3.   print("Above 100")  
  4.   if a > 1000:  
  5.     print("and also above 1000")  
Output
 
Conditional Statements In Python
 

Nested if else statement

 
In Python is using one “if else” statement inside other  if else statements it is called nested if else statement.
 
Syntax
  1. if(condition):  
  2. {  
  3.      if(condition):  
  4.      {  
  5.        #if statement  
  6.      }  
  7.      else:  
  8.      {  
  9.         #else statement  
  10.      }  
  11. }  
  12. else:  
  13. {  
  14.  #else statement  
  15. }  
Example
 
Program for nested if else statement,
  1. a=int(input("enter the a value"))#user give a value  
  2. if a> 100:  
  3.   print("Above 100")  
  4.   if a > 1000:  
  5.     print("and also above 1000")  
  6.   else:  
  7.     print("and also below 1000")  
  8. else:  
  9.     print("below 100")  
Output
 
Conditional Statements In Python
 

Conclusion

 
In this article, we have seen conditional statements. I hope this article was useful to you. Thanks for reading!


Similar Articles