Loading...

Python If...Else


in this tutorial we are going to learn decision making statements based on condition statements
these are if...else statement and if...elif...else statements


What is if...else in python ?


if...else, if...elif...else are conditional statements in python.
in writing python programs there may be a situation arise where we want to make decisions depends upon the conditions.
the program has to take a decision which block of code will execute if the certain condition is met.
in that case these conditional statements comes to rescue for making accurate decisions in the program.


if...else statements


in python the syntax of the if...else statements is given below
        		     					    		
if test_expression:
    statements to be execute
else:
    statements to be execute    
    	    		     					    	
	        		     					    
after evaluating the test_expression value
if the value returns true then the statements in the 'if' block will execute.
if the value returns false then the statements in the 'else' block will execute.
for example:
        		     					    		
x = 49

y = 73

if x < y:
    print("x is less than y")
else:
    print("x is greater than y")

output:

x is less than y	    
    	    		     					    	
	        		     					    
in the above example we take two variables x and y in which we have assign the values x = 49 and y = 73.
in python, when the program evaluates the expression (x < y) value
then the program will understand that the value in the variable x is less than the value in the variable y.
then the program will print on the screen " x is less than y ".


combine conditional if...else statements


in python we can combine conditional expressions by using logical operators and, or


And


in python and is a keyword
the combine conditional expression will return true if both conditions are true
if any of the condition is false then the conditional expression will return false
for example:
        		     					    		
x = 97
y = 251
z = 350

if x < y and x < z:
    print("x is smaller than both y and z")
else:
    print("combine conditions are false")   
	
output:

x is smaller than both y and z
    	    		     					    	
	        		     					    
in the above program both conditions are true then the combine conditional expression in if part returns true.
so it prints on the screen " x is smaller than both y and z "


Or


in python or is a keyword
the combine conditional expression will return true if atleast one condition is true
if both the condition is false then the conditional expression will return false
for example:
        		     					    		
x = 97
y = 251
z = 350

if x < y or x > z:
    print("only one conditions is true")
else:
    print("bothconditions are false")   
	
output:

only one conditions is true
    	    		     					    	
	        		     					    
in the if part only first condition is true and second condition is false
so it prints on the screen " x is smaller than both y and z "


if...elif...else statements


in python the syntax of the if...elif...else statements is given below
        		     					    		
if test_expression:
    statements to be execute
elif test_expression:
    statements to be execute
else:
    statements to be execute        
    	    		     					    	
	        		     					    
in python the conditions will be check from top to bottom approach.
the program finds the true values first in the if part then in the elif part.
if true values does not exist in if...elif then it will finally execute else part statements
for example:
        		     					    		
x = 49

y = 73

if x == y:    
	print("both x and y are equal")
elif x > y:
    print("x is greater than y")
else:
    print("y is greater than x")	

output:

y is greater than x	    
    	    		     					    	
	        		     					    
in the above example we take two variables x and y in which we have assign the values x = 49 and y = 73.
in python, when the program evaluates the expression (x < y) value
first it checks in if part - x is equals to y which is false.
then in elif part - x is greater than y which is false.
finally executes else part on the screen " y is grater than x "


Compare combine conditional if...else statements


in python the syntax of the if...elif...else statements is given below
        		     					    		
if test_expression:
    statements to be execute
elif test_expression:
    statements to be execute
else:
    statements to be execute        
    	    		     					    	
	        		     					    
in python the conditions will be check from top to bottom approach.
the program finds the true values first in the if part then in the elif part.
if true values does not exist in if...elif then it will finally execute else part statements