Exploring New Techs!

Python - Conditional Statements

 


Hello Technotizers, this article focuses on Python Conditional Statements and their uses. Python Conditional Statements are the If…else statements that are used for evaluating conditions and performing certain task based on it. Various Python operators are used in conditional statements.


  • ·         If statement:

If statement is written using the if keyword. If the condition given in the if statement is true, then the code block be next to the if statement gets executed. Indentation has to be kept in mind while writing if..else statements.

Let’s see an example:

x = 14                          
y = 4                           
if x>y:                         
    print("x is greater than y")


The output is obtained as follows:

x is greater than y 

Here, x is greater than y i.e. 14 is greater than 4 which is true and hence the code block is executed and “x is greater than y” is printed.


  • ·         Elif statement:

Elif is a combination of if and else statements. Elif says that, “if the previous conditions were not true then try this one”.

Let’s see an example:

x = 14                          
y = 14                          
if x>y:                         
    print("x is greater than y")
elif x==y:                      
    print("x is equal to y")    


The output is obtained as follows:

x is equal to y

Here, x is not equal to y so that the if condition becomes false and the code block gets skipped. Further the elif condition is checked and it is true as x is equal to y i.e. 14 is equal to 14 and hence the code block is executed and “x is equal to y” is printed.


  • ·    Else statement:

 Else statement executes a code block if all the previous statements are false.

Let’s see an example:

x = 4                           
y = 14                          
if x>y:                         
    print("x is greater than y")
elif x==y:                      
    print("x is equal to y")    
else:                           
    print("y is greater than x")


The output is obtained as follows:

y is greater than x

 Here, the if and elif conditions are false hence the else code block gets executed and “y is greater than x” is printed.


  • ·    Short Hand If statement:

This is used to write the if statement in one line and not as a code block.

Let’s see an example:

x = 14                              
y = 4                               
if x>y: print("x is greater than y")

This code will give the same output as before.


  • ·    Short Hand If…else statement:

 This is used to write the if and else statements combined in one line and not as a code block.

Let’s see an example:

x = 4                                                                   
y = 14                                                                  
print("x is greater than y")if x>y else print("y is greater than x")    

This code will print “y is greater than x” on the screen.

 

  • ·    And operator:

The and keyword is used in if…else statements to combine conditions and obtain the truth value. If the both or all the conditions are true then only the truth value is considered as true.

Let’s see an example:

x = 14                               
y = 4                                
z = 10                               
if x>y and x>z:                      
    print("Both conditions are true")


The output is obtained as follows:

Both conditions are true

 

Here, as mentioned in the if condition, the value of ‘x’ is greater than both ‘y’ and ‘z’, hence the truth value is True and code block is executed.

 

  • ·    Or operator:

The or keyword is used in if…else statements to combine conditions and obtain the truth value. If the one of all the conditions is true then only the truth value is considered as true.

Let’s see an example:

x = 14                                     
y = 4                                      
z = 20                                     
if x>y or x>z:                             
    print("At least one condition is True")

 

The output is obtained as follows:

At least one condition is True

 

Here, as mentioned in the if condition, the value of ‘x’ is greater than ‘y’ but not greater than ‘z’, hence only one condition is True. The truth value is of the statement is True and code block is executed.

 

  • ·   Nested If:

A if statement inside another if statement is called a nested if.

Let’s see an example:

x = 14                             
if x > 10:                         
    print("Above 10")              
    if x > 20:                     
        print("and also above 20!")
    else:                          
        print("but not above 20."

 

The output is obtained as follows:

Above 10         
but not above 20.

 

Here, if the outer if statement gets true then the inner if statements are executed and this goes on up till all the statements are executed.

 

  • ·   The pass statement:

If statements cannot be empty but for any reason you have an if statement with no content then put the pass keyword to avoid getting an error.

Let’s see an example:

x = 14  
y = 4   
if x>y: 
    pass

This code returns no output on the screen but also avoids getting an error.

With this we come to an end of this article. Hope it was helpful. Do provide your feedback and ideas through comments, it would be highly appreciated. See you soon!

Keep coding and exploring new techs!!

 

 

0 comments:

Post a Comment