If and Else If and Else Statement in Python

If-elseif and else statement in Python programming allows you to control and check conditions and execute the specific block of code if the condition is true, otherwise, execute the else block.

Program Using If Elif and Else Statement

Program

num = int(input("Enter a number less than 10: "))
if num > 10: #if 'num' value is greater then 10 then
    print("Entered value is greater then 10!") #print this.
elif num < 10: #if 'num' value is Less then 10 then
    print("Entered value is Less then 10!") #print this.
else: #if not in above mean equal to 10 then
    print("Entered value is Equal to 10") #print this.
Output
Enter a number less than 10: 52
Entered value is greater then 10!

Program Using If Elif and Else Checking Name

Program

n = input("Enter Ur Name: ")
if n.endswith("Ls"): #if 'n' value Ends with 'Ls' then
    print("Hello HiLaLs") #print this.
elif n.startswith("Af"): #if 'n' value Starts with 'Af' then
    print("Hello Afridi") #print this.
else: #if Not then
    print("Invalid Entry") #print this.
Output
Enter Ur Name: Bilal Khan
Invalid Entery

Program Working with If Elif and Else Statement

Program

num = input("Please Enter Ur Name: ")
if num == "HiLaLs": #if 'num' value equal to 'HiLaLs' then
    print("Hi Programmer!") #print this.
elif num == "Nawaz": #else if 'num' value equal to 'Nawaz' then
    print("Hi Web Developer") #print this.
elif num == "Amir": #else if 'num' value equal to 'Amir' then
    print("Hi Network") #print this.
elif num == "Asad": #else if 'num' value equal to 'Asad' then
    print("Hi DataBase") #print this.
else: #if not in above then
    print("I Dont Know You") #print this.
Output
Please Enter Ur Name: HiLaLs
Hi Programmer!

Working with If Elif and Else Statement using OR Operator
Program

val = input("Enter Ur Name: ")
if val == "HiLaLs" or val == "Amir": #if 'val' value Equal to 'HiLaLs' OR 'Amir'
    print("Hi Programmer!") #then print this.
elif val == "Nawaz" or val == "Ali" or val == "Hedayat": #if 'val' value Equal
    print("Hi Web Developer!") #to 'Nawaz' OR 'Ali' OR 'Hedayat' then print this.
elif val == "Sadam" or val == "Usman": #if 'val' value Equal to 'Sadam' or
    print("Hi Networkers!") #'Usman' then print this.
else: #if not then
    print("I Dont Know You") #print this.
Output
Enter Ur Name: Bilal Khan
I Dont Know You

Program Using If Elif and Else in If Statement
Program

num = int(input("Enter value from 1 to 3: "))
if num <= 3: #if 'num 'value is Less then or Equal to 3 then
    if num ==1: #if 'num' value equal to 1 then
        print("Entered value is 1") #print this.
    elif num == 2: #if 'num' value equal to 2 then
        print("Entered value is 2") #print this.
    elif num == 3: #if 'num' value equal to 3 then
        print("Entered value is 3") #print this.
    else: #if not then
        print("Entered value is Less then 1") #print this.
else: #if not then
    print("Invalid Entry!") #print this.
Output
Enter value from 1 to 3: 2
Entered value is 2