Python elif Statement

The Python elif Statement is also called the Else If, and elseif is very useful when we have to check several conditions. Apart from this Python elif, we can also use the Nested If to achieve the same. However, as the number of conditions increases, the Nested If else complexity will also increase. Let us see the syntax of the same.

Python Elif Syntax

The syntax of Python elif or else if statement is

if (condition 1):
    statements 1
elif (condition 2):
    statements 2
    ...........
elif (condition n):
    statements n
else:
    default line

The Python Elif or else if statement handles multiple lines effectively by executing them sequentially. It means the elif will check for the first condition, if the condition is TRUE then it will execute the statements present in that block.

If the condition is FALSE then Python elseif will check the Next one (Elif or else if conditional statement) and so on. There will be some situations where condition 1, and condition 2 is TRUE, for example:

x= 20, y=10

Condition 1: x > y # TRUE

Condition 2: x != y # TRUE

In these situations, a block of code under Condition 1 will execute. Because the elif conditions will only be executed if its previous if statement (else) fails.

Elseif Flow Chart

The flow chart of this Python elif or elseif conditional statement is

Python Else IF, Elseif or Elif Flow Chart

Python elif Example

In this elseif program, the User is asked to enter his total 6 subject marks. Using the Python Elif or else if statement we check whether they are eligible for scholarships or not.

# Imagine you have 6 subjects and Grand total is 600
Totalmarks = int(input(" Please Enter Your Total Marks:  "))
if Totalmarks >= 540:
    print(" Congratulations! ")
    print(" You are eligible for Full Scholarship ")
elif Totalmarks >= 480:
    print(" Congratulations! ")
    print(" You are eligible for 50 Percent Scholarship ")
elif Totalmarks >= 400:
    print(" Congratulations! ")
    print(" You are eligible for 10 Percent Scholarship ")
else:
    print(" You are Not eligible for Scholarship ")
    print(" We are really Sorry for You ")

Once you complete it, Please save the Python file. Once you save the file, Let us hit F5 to run the script. The shell will pop up with the message “Please Enter Your Total Marks:” .

OUTPUT 1: We are going to enter Totalmarks = 570. First If condition is TRUE. So, elseif output is displaying the print code inside the If statement.

Please Enter Your Total Marks: 570
Congratulations!
You are eligible for Full Scholarship

This time, let me test the Python elif or elseif statement. For this, we are going to enter Totalmarks to 490 means first IF expression evaluating FALSE. It will check the (Totalmarks >= 480), which is TRUE so the program will print the code inside this block. Although else if (Totalmarks >= 400) condition is TRUE, it won’t check this condition.

Please Enter Your Total Marks: 490
Congratulations!
You are eligible for 50 Percent Scholarship

Output 3: This time we entered Totalmarks as 401 means the first IF condition, else if (Totalmarks >= 480) are FALSE. So, It will check the (Totalmarks >= 401), which is TRUE. So elseif or elif returns the block of code.

Please Enter Your Total Marks: 401
Congratulations!
You are eligible for 10 Percent Scholarship

OUTPUT 4: We entered the Totalmarks as 380 means all the IF conditions Fail. So, the elseif prints the else block.

Python Elif Statement 7

In this Python elif or elseif program, first, we declared Totalmarks to enter any integer value.

Totalmarks = int(input(" Please Enter Your Total Marks:  "))

If the person’s Total marks are greater than or equal to 540, then the following lines will display

print(" Congratulations! ")
print(" You are eligible for Full Scholarship ")

If the first elseif condition of the elif fails then it will go to the second. And, if the person’s Total mark is greater than or equal to 480 then the following code will display.

print(" Congratulations! ")
print(" You are eligible for 50 Percent Scholarship ")

When the first and second condition of the Python Elif, elseif, or Else if fails then it will go to third. If the person’s Total marks are greater than or equal to 400 then the following statements will be displayed.

print(" Congratulations! ")
print(" You are eligible for 10 Percent Scholarship ")

If all the above statements in the elseif statement fail, then it will go to the else block and print the following statements. Please refer to the Nested If article.

print(" You are Not eligible for Scholarship ")
print(" We are really Sorry for You ")