The Python Elif Statement also called as the Python Else If Statement 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 increase, Nested If code complexity will also increase. Let us see the syntax of Elif statement in python:
Python Elif Syntax
The syntax of Python Elif statement is as follows:
if (condition 1): statements 1 elif (condition 2): statements 2 elif (condition 3): statements 3 ........... elif (condition n): statements n else: default statements
The Elif python statement handle multiple statements effectively by executing them sequentially. It 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 it will check the Next one (Elif condition) and so on.
There will be some situations where condition 1, condition 2 is TRUE, for example:
x= 20, y=10
Condition 1: x > y # TRUE
Condition 2: x != y # TRUE
In these situations, statements under the Condition 1 will be executed because Python ELIF conditions will only be executed if its previous IF or ELIF statement fails.
Python Elif Statement Flow Chart
The flow chart of Python Elif statement is as shown below
Python Elif Example
In this elif in python program, User is asked to enter his total 6 subject marks. Using Python Elif statement we are going to calculate whether he/she is eligible for scholarship or not. In order to do this, Please open your favorite IDLE to write Python script and here we are using Python 3.5.0.
Once you open Python IDLE, Please select the New File as shown in the below screenshot or else simply click Control + N
Once you click on the New File, new file window will be opened to write Python Elif statement script. Please add the following script in that new file
# Example for Python Elif Statement # 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 are done, Click on the File Menu and select Save option as shown below
Please save the file as per your requirements
Once you save the Python Elif file, Let us run the script and see by selecting Run Menu and clicking the Run Module or simply click F5
Once you click on Run Module, Our Python shell will pop up with message “Please Enter Your Total Marks:” .
OUTPUT 1: We are going to enter Totalmarks = 570. First If condition is TRUE. So, output is displaying the print statements inside the If statement
OUTPUT 2: This time, let me test the elif python statement. For this, we are going to enter Totalmarks to 490 means first IF condition is FALSE. It will check the elif (Totalmarks >= 480), which is TRUE so it will print the statements inside this block. Although else if (Totalmarks >= 400) condition is TRUE, but it won’t check this condition.
OUTPUT 3: This time we entered Totalmarks as 401 means first IF condition, else if (Totalmarks >= 480) are FALSE. So, It will check the else if (Totalmarks >= 401), which is TRUE so it returns the statements within this block.
OUTPUT 4: We entered the Totalmarks as 380 means all the IF conditions Fail. So, It returns the statements inside the else block.
In this elif program, first, we declared Totalmarks variable and asks the user to enter any integer value. int() restrict the user not to enter non integer values
Totalmarks = int(input(" Please Enter Your Total Marks: "))
Within the Python Elif statement, If the person Total marks is greater than or equal to 540 then following statements will print
print(" Congratulations! ") print(" You are eligible for Full Scholarship ")
If the first condition of the Elif fails then it will go to second statement. And, if the person Total marks is greater than or equal to 480 then following statements will printed
print(" Congratulations! ") print(" You are eligible for 50 Percent Scholarship ")
When the first and second condition of the Python Elif fails then it will go to third statement. If the person Total marks is greater than or equal to 400 then following statements will be printed
print(" Congratulations! ") print(" You are eligible for 10 Percent Scholarship ")
If all the above statements in the elif statement fails then it will go to else block and print following statements
print(" You are Not eligible for Scholarship ") print(" We are really Sorry for You ")