Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
If else Statement

Python if...else Statement

 

if condition:
    Body of if
else:
    Body of else

 

The if..else statement evaluates the condition and will execute the body of if only when the condition is True.

 

If the condition is False, the body of else is executed. Indentation is used to separate the blocks.

 

Example:

 

#Check whether the given number is even or odd.
 
n = int(input())
 
if n % 2 == 0:
    print(n,"is even.")
else:
    print(n,"is odd.")

 

output: 

 

7
7 is odd.

 

In the above example if n is divisible by 2 means the remainder will be zero then the condition will be true and “is even” will be output else “is odd” will be output.