Python if-else Statement with example and syntax by - SHADBOX.COM

Total Blog Views: 49

Blog Status: publish

Created By: swazahmad Created at: 07-24-2021

Tags: python

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
     >>> 
 
 
 


swazahmad

Swaz is Practice Lead of ROR Developer. he's a Blogger, Mentor, and Rubyst. He has expertise in Javascript also . he's highly skilled in designing, developing and testing web applications. He enjoys being challenged and interesting with projects that need him to



Comments



  • swazahmad | almost 4 years ago
    Awesome Information Thanks For Sharing
Buy traffic for your website

About Shadbox

we have the “Get things executed” lifestyle at our place of work. There are not any excuses, no if’s or however’s in our dictionary. committed to navigating the ship of creativity to create cell answers, we resolve the real-lifestyles troubles of our clients and their clients. Our passion for work has won us many awards, year after 12 months.

Services

Downloads