What is an Elif Statement in Python?

When you have numerous procedures to complete, the Elif statement (also known as else if ) can assist you to find a solution. A certain operation should be done depending on whatever condition is satisfied, but not all at the same time. The else-if statement, which is a hybrid of the if and else statement, is used to do this.

  • A conditional statement that decides the program’s behavior depending on a logical circumstance.

  • The logical condition is true or false, similar to the if statement.

  • It must be used in combination with an if statement.

Important to note:

  • Multiple Elif statements can be used.

  • The Elif statement (s) will not be performed if the logical expression in the if statement is true.

  • When an Elif statement is generated, it must be followed by an else statement. This allows the program to continue even if none of the if conditions are fulfilled strong text

Example:
Using the elif statement to show the corresponding day by assigning a number to a variable num(1- Monday, 2- Tuesday 3- Wednesday…).

num=7

if num==1:
    print('Monday');
elif num==2:
    print('Tuesday')
elif num==3:
    print('Wednesday')
elif num==4:
    print('Thursday')
elif num==5:
    print('Friday')
elif num==6:
    print('Saturday')
elif num==7:
    print('Sunday')
else:
    print('Invalid input, please enter 1 to 7 only')

Output

day bi