Programming is mostly about executing statements with certain conditions or iterating through block of statements. It is important in a program to check conditions and control the flow of statement execution accordingly. Python If else statement allows this ability to programs like other programming languages.
Note: Python blocks are created by indenting the statements. In Python If Else statements also you have to use indentation to define a block of statements to be executed after the condition.
The demonstration examples use random number. Random package is imported and its randint function is used to generate a number by specifying the range in parenthesis.
Python If else Syntax
The if else conditional statement in Python can be written in many ways. It can be used as if, if-else or if-elif-else and nested if-else.
These are its different forms
Form 1- using if
if Boolean-condition-statement:
statements to be executed when the Boolean-condition-statement is true

In this form of conditional statements the Boolean-condition-statement is evaluated. If it is true the following statements are executed else the statements after if block are executed.
Example
import random # import random package to generate random number
num=random.randint(0,20) # generating random number between 0-20
if num>10:
print('number is greater than 10')
print('generated number is',num)
Output of two executions

Form 2- using if-else
if Boolean-condition-statement:
statements to be executed when the Boolean-condition-statement is true
else:
statements to be executed when the Boolean-condition-statement is false

In this form of conditional statement the Boolean-condition-statement is evaluated. If it is true the following statements are executed else the statements after else: are executed.
Example
import random # import random package to generate random number
num=random.randint(0,20) # generating random number between 0-20
if num>10:
print(num,'number is greater than 10')
else:
print(num,'generated number is less than 10)
Output of three executions

Form 3- using if-elif-else
if Boolean-condition-statement1:
statements when the Boolean-condition-statement1 is true
elif Boolean-condition-statement2:
statements when the Boolean-condition-statement2 is true
else:
statements to be executed when the previous Boolean-condition-statement is false
In this form of conditional statement the Boolean-condition-statement is evaluated. If it is true the following statements are executed else the condition the elif Boolean-condition-statement is evaluated. If it the true its following statement block is executed. When elif Boolean-condition-statement is false then next elif condition is checked and the process continues. else: is the last block of if-elif-else which is executed when the last elif Boolean-condition-statement evaluates to false
Example
import random # import random package to generate random number
num=random.randint(1,7) # generating random number between 0-20
if num==1:
print('Monday mad')
elif num==2:
print('Tuesday Tired')
elif num==3:
print('Wednesday weird')
elif num==4:
print('Thursday Thunderous')
elif num==5:
print('Friday Fun')
elif num==6:
print('Saturday Smiling')
else:
print('Sunday Sulking')
Output of three executions

Form 4- Nested Python if-else
if Boolean-condition-statement1:
if Boolean-condition-statement2:
statements when the Boolean-condition-statement2 is true
else:
statements when the Boolean-condition-statement2 is false
else:
statements when the Boolean-condition-statement1 is false

Nesting of Python if-else statements gives you the flexibility to add conditions within conditions. The nesting can be done both at if part and else part
Example
import random # import random package to generate random number
num=random.randint(0, 100) # generating random number between 0-20
print('The randomly generated number is', num)
if num<=50:
if num<25:
print('First Quarter')
else:
print('Second Quarter')
else:
if num<75:
print('Third Quarter')
else:
print('Fourth Quarter')
Output of four executions

Be First to Comment