Python if-else Statement

Python if-else Statement

In all programming language if-else statement is used to test a particular condition and if the condition is true ,it execute the if block and the condition is false ,it execute the else block. 


In python decision making is done by the following statement :
  1. If statement
  2. If else statement
  3. Nested if statement 

1.If statement :  

                    if statement is used to check the condition if condition is true it execute the particular code of block.

Syntax :

             if expression :
                     Statements(Conditions)

Examples :

     >>> a =10
     >>> if a == 10:
     print("true")

     true
     >>> 

2.If else statement :

                 if-else statement provide an else block, if statement condition is false then else condition is  execute. if the condition of if block is true ,then the if block is execute.otherwise else part executed.

Syntax :           

             if expression :
                       Statements(Conditions) # if condition  block
             else:
                       Statements(Conditions) # else condition block

Example :


     >>> a =100
     >>> if a != 100:
     print("true")
     else:
     print("false")

     false
     >>>

Nested if statement :

                    the elif Statement enable us to check multiple condition and execute the specific block of statement depending upon the true condition among them.

Syntax :           

             if expression 1:
                       Statements(Conditions)
             elif expression 2:
                       Statements(Conditions)  
             elif expression 3:
                       Statements(Conditions)  
             elif expression 4:
                       Statements(Conditions)  
             else:
                       Statements(Conditions)  


Example :

     >>> a =100
     >>> if a ==40 :
         print("number is 40")
     elif a == 50:
         print("number is 50")
     elif a == 100:
         print("numer is 100")
     else:
         print("number is not present")

     numer is 100
     >>> 

            
        

Comments

Post a Comment