Conditional statements in Python

Conditional Statements: It tests a condition and executes a particular statement or a group of statements depending upon the result of the condition. It is a compound statement i.e. it contains other statements. Python supports following types of conditional statements
1) if
2) if else
3) if elif else
4) Nested if else

if statement: It tests a condition and executes a particular statement or a group of statements only if the result of the condition is True. If the condition is False, it will not do anything. It is a compound statement i.e. it contains other statements.
syntax is:

if(condition):
statement or a group of statements in an indented block

Flowchart of if statement is :

Program to check and print if a candidate is eligible for voting.

age=int(input("enter age "))
if(age>=18):
    print("you are eligible for voting")

Output Run 1:

enter age 18
you are eligible for voting

Output Run 2:

enter age 50
you are eligible for voting

Output Run 3:

enter age 17

if else statement: It tests a condition and executes a particular statement or a group of statements only if the result of the condition is True. If the condition is False, it will execute another statement or a group of statements. It is a compound statement i.e. it contains other statements.
syntax is:

if(condition):
statement or a group of statements in an indented block

else:
another statement or a group of statements in an indented block

Flowchart of if else statement is :

Program to check and print if a candidate is eligible/not eligible for voting.

age=int(input("enter age"))
if(age>=18):
    print("you are eligible for voting")
else:
    print("you are not eligible for voting")

Output Run 1:

enter age 18
you are eligible for voting

Output Run 2:

enter age 50
you are eligible for voting

Output Run 3:

enter age 17
you are not eligible for voting

Program to find the absolute value of a number

n=int(input("Enter number "))
if(n>=0):
    print("Absolute value of the number is", n)
else:
    print("Absolute value of the number is", -n)

Output Run 1:

Enter number 5
Absolute value of the number is 5

Output Run 2:

Enter number -5
Absolute value of the number is 5

Output Run 3:

Enter number 0
Absolute value of the number is 0

if elif else statement: It tests condition 1 and executes first statement or a group of statements if the result of the condition 1 is True. If the condition 1 is False, It tests another condition 2 and executes second statement or a group of statements if the result of the condition 2 is True. If the condition 2 is False, It tests another condition 3 and executes third statement or a group of statements if the result of the condition 3 is True and so on. It will execute statement or a group of statements following else part if all the conditions are False. We can also skip this else block as it is optional part. If we skip the else block, then nothing will be done if all the statements are False. It is a compound statement i.e. it contains other statements.
syntax is:

if(condition1):
first statement or a group of statements in an indented block

elif(condition2):
second statement or a group of statements in an indented block

elif(condition3):
third statement or a group of statements in an indented block

else:
another statement or a group of statements in an indented block

Flowchart of if elif else statement is :

Example: Program to find the absolute value of a number

n=int(input("Enter number "))
if n>0: 
    print("Absolute value of the number is", n)
elif n<0:
    print("Absolute value of the number is", -n)
else:
    print("Absolute value of the number is", n)

Output Run 1:

Enter number 5
Absolute value of the number is 5

Output Run 2:

Enter number -5
Absolute value of the number is 5

Output Run 3:

Enter number 0
Absolute value of the number is 0

Nested if else: It contains an if or if-else block within if/else/both if and else/elif parts. Syntax is:

if(condition1):
       if(condition2):
             first statement or a group of statements in an indented block
       else:
             second statement or a group of statements in an indented block
else:
       if(condition3):
             third statement or a group of statements in an indented block
        else:
            fourth statement or a group of statements in an indented block

Program to sort 3 numbers

a=int(input("Enter first number "))
b=int(input("Enter second number "))
c=int(input("Enter third number "))

if a>b and a>c: 
    if b>c:
        max,mid,min=a,b,c
    else:
        max,mid,min=a,c,b
        
elif b>a and b>c: 
    if a>c:
        max,mid,min=b,a,c
    else:
        max,mid,min=b,c,a

elif c>a and c>b: 
    if a>b:
        max,mid,min=c,a,b
    else:
        max,mid,min=c,b,a

print("Numbers in sorted order (Descending Order) are" ,max , mid, min)
print("Numbers in sorted order (Ascending Order) are" ,min , mid, max)

Output Run 1:

Enter first number 4
Enter second number 19
Enter third number 3
Numbers in sorted order (Descending Order) are 19 4 3
Numbers in sorted order (Ascending Order) are 3 4 19

Output Run 2:

Enter first number 50
Enter second number 10
Enter third number 20
Numbers in sorted order (Descending Order) are 50 20 10
Numbers in sorted order (Ascending Order) are 10 20 50

Output Run 3:

Enter first number 20
Enter second number 30
Enter third number 60
Numbers in sorted order (Descending Order) are 60 30 20
Numbers in sorted order (Ascending Order) are 20 30 60

Program to check divisibility of a number by another number

a=int(input("Enter first number "))
b=int(input("Enter second number "))

if a % b == 0:
    print("Number " , a ," is divisible by " ,b)
else:
    print("Number " , a , " is not divisible by " ,b)

Output Run 1:

Enter first number 10
Enter second number 3
Number  10  is not divisible by  3

Output Run 2:

Enter first number 10
Enter second number 5
Number  10  is divisible by  5
error: You can only copy the programs code and output from this website. You are not allowed to copy anything else.