Introduction to else if Statement in Python
The ‘if’ condition statement is the most frequently used condition statement in python programming. The ‘if’ statement is used to evaluate whether a set of code needs to be executed or not. The flow happens like , if the statement is true then execute a below code set , when false then move into the next code set. here the role of the else if statement is to get re evaluated with a different set of condition when the if condition check fails.
Below are the syntax for the else if Statement in Python:
elif condition statement:
. . .
. . .
else-if Code block
Explanation: The else-if statement in python is represented as elif. where ‘el’ abbreviates as else and ‘if ‘ represents normal if statement. the condition which is going to be evaluated is presented after the else-if statement. the colon ( ‘ : ‘ ) is used to mention the end of condition statement being used. the code block which needs to be executed when the else-if statement is successful is placed after the else if condition.
Explanation: The Flow starts with a if statement check, if the statement check fails then the control flows to the else if statement check, when the else if statement check evaluates as true then the flow goes to body of the else if statement.
How does else if Statement work in Python?
The procedure in which else if statement works is as below:
4.8 (4,708 ratings)
- Check for the initial if statement.
- If the firstly mentioned if statement is evaluated as factual , then execute the body of the if statement directly.
- In case the firstly mentioned if statement is false then the control flows to the else section of the program.
- When the else section of the program holds a if statement with it then it becomes a else-if statement.
- The else if statement’s code block will be executed when the condition stated in the elif statement evaluates as true.
- In case if this condition is evaluated as false then the condition flows to the end of the if block and starts executing the code segment after the if block.
Examples to Implement else if Statement in Python
Examples to implement else if Statement in Python as below,
# Python program to illustrate if-elif-else
# Variable Declaration section
check_variable = 20
# Program logic section
if (check_variable == 2):
print ("check_variable is 2")
elif (check_variable == 4):
print ("check_variable is 4")
elif (check_variable == 6):
print ("check_variable is 6")
elif (check_variable == 8):
print ("check_variable is 8")
elif (check_variable == 10):
print ("check_variable is 10")
elif (check_variable == 12):
print ("check_variable is 12")
elif (check_variable == 14):
print ("check_variable is 14")
elif (check_variable == 16):
print ("check_variable is 16")
elif (check_variable == 18):
print ("check_variable is 18")
elif (check_variable == 20):
print ("check_variable is 20")
print ("check_variable is not present")
Explanation: This program is coded in such way to depict the flow of control in if statements in python programming. As noticed the program consists of multiple else-if blocks , the program logic explanation is as below,
- The check variable is assigned with value 20.
- From check variable value is verified for an exact match against all even number falling between 2 to 20.
- When a exact match is deducted then the value is printed in the console.
from country_list import countries_for_language
from collections import deque
# extract all the values of the countries
Nation_dictionary = dict( countries_for_language( 'en' ) )
#Add all the country names to a stack
Nation_stack_variable = deque()
for i in Nation_values:
print( ' Type of stack variable used : ' , type(Nation_stack_variable),'\n')
print( ' Vales of the stack variable : ' , Nation_stack_variable ,'\n')
Nation_temp = Nation_stack_variable.pop()
if Nation_temp == 'A':
elif Nation_temp == 'Z':
print( ' \n Stack variable values after Pop : ' , Nation_stack_variable , '\n')
Explanation: Here the program utilizes a collection data type for organizing the stack. this procedure brings in the deque class which belongs to the collection library. At this point the ‘ country_list ‘ library import is accountable for dragging the list of all the countries. All values from the country list library is taken out into a dictionary variable since the country library extract is in the format of dictionary. The dictionary arrangement where the first alphabet is a notation for the key beneath which the exact country name is located.
Input_value = int(input(" Factorial Number : "))
factorial = 1
if Input_value < 0:
print(" Negative number cannot be placed for factorial determination ")
elif Input_value == 0:
print(" No factorial value for zero ")
for i in range(1,Input_value + 1):
factorial = factorial*i
print("Value for the factorial " , Input_value , "is" , factorial)
Explanation: The factorial value of a given number is calculated by means of looping technique ,The ‘Input_variable’ is used for fleeting the integer value intended for which the factorial value is accepted to be calculated. Also the variable ‘Factorial’ is set to initial value of 1. At first the keyed in value is verified whether it is a positive integer or not, in case if the integer is a negative value then this is notified in the console using a print statement, the necessity for maintaining the input value as positive is because the factorial value for a negative integer does not exist. so the check is implied such that the keyed in value is greater than zero. Also the next check ensures that the keyed in value is not zero.
For optimized execution every programming language relies on its looping and condition statements. Here these condition statements like else if helps to attain better control over the program logic execution.
This is a guide to else if Statement in Python. Here we discuss Syntax to else if Statement in Python, how does it work,examples with codes and outputs. You can also go through our other related articles to learn more –