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. If the statement is true, the flow happens like executing a below code set; when false, then move into the next code set. Here the else’s role if statement is to get reevaluated with a different set of conditions when the condition check fails.
Syntax
Below is 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 the 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.
Flowchart
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 the 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:
- Check for the initial if statement.
- If the first mentioned if statement is evaluated as factual, then execute 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 elif statement’s condition 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 of implementing else if Statement in Python as below,
Example #1
Code:
# Python program to illustrate if-elif-else
#!/usr/bin/python
# 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")
else:
print ("check_variable is not present")
Output:
Explanation: This program is coded in such a 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 the value 20.
- From check, the variable value is verified for an exact match against all even number falling between 2 to 20.
- When an exact match is deducted, then the value is printed in the console.
Example #2
Code:
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' ) )
Nation_values =Nation_dictionary.values()
#Add all the country names to a stack
Nation_stack_variable = deque()
for i in Nation_values:
Nation_stack_variable.append(i)
print( ' Type of stack variable used : ' , type(Nation_stack_variable),'\n')
print( ' Vales of the stack variable : ' , Nation_stack_variable ,'\n')
while Nation_stack_variable:
Nation_temp = Nation_stack_variable.pop()
if Nation_temp[0] == 'A':
print(Nation_temp)
elif Nation_temp[0] == 'Z':
print(Nation_temp)
print( ' \n Stack variable values after Pop : ' , Nation_stack_variable , '\n')
Output :
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 all the countries’ list. All values from the country list library are taken out into a dictionary variable since the country library extract is in a dictionary format. The dictionary arrangement where the first alphabet is a notation for the key beneath which the exact country name is located.
Example #3
Code:
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 ")
else:
for i in range(1,Input_value + 1):
factorial = factorial*i
print("Value for the factorial " , Input_value , "is" , factorial)
Output:
Explanation: The factorial value of a given number is calculated by means of the 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 an 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.
Conclusion
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.
Recommended Articles
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 –
40 Online Courses | 13 Hands-on Projects | 215+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses