If the ‘if’ condition becomes False, the very next ‘elif’ portion’s condition is evaluated. The content of ‘else’ would be implemented when all of the requirements become False. As per the situation, just one of the numerous if…elif…else statements is evaluated. There would only be another clause in the ‘if’ section. This could, however, have several ‘elif’ statements. We are going to explain the ‘elif’ python in this article.
Example no 1:
We will evaluate a defined number to see whether that value is a positive number or zero or a non-positive number.
num = 5.8
if num > 0:
elif num == 0:
We will initialize a variable ‘num’ and assign it a value having a quebrado point in it. We have utilized the ‘if’ statement. Within the ‘if’ statement, we use the condition that num>0. It shows that if the required value is greater than 0, the print() command prints the message ‘Positive number’. In the next line, we use the ‘elif’ statement. Here, we apply condition num==0. It represents that if the defined number is equal to 0, then it prints the text ‘Zero’. At the end, within the else statement, the print() function is being used to display the line ‘Negative number’.
As the specified value is greater than 0, the print() statement prints the text ‘Positive number’.
Example no 2:
The ‘elif’ statements are employed just after the if expression in this instance. Python can assess the ‘if’ statement. If it returns False, this would analyze the ‘elif’ statements and implement the ‘elif’ statement with the True representative. If more than one ‘elif’ statement is fulfilled, the very first ‘elif’ section is invoked.
rate = 5000
if rate > 5000:
print(«rate is greater than 5000»)
elif rate == 5000:
print(«rate is 5000»)
elif rate < 5000:
print(«rate is less than 5000»)
First of all the variable named ‘rate’ is being declared and then the value is assigned to it. To determine information about the provided value, we use conditions. We apply the criterion rate>5000 within the ‘if’ expression. The print() function prints the message ‘rate is greater than 5000’ if the defined value is greater than 5000. The ‘elif’ expression is being used in the next line. The requirement rate==5000 is used here. It indicates that if the specified value is equal to 5000, the text ‘rate is 5000’ will be displayed by using the print() function.
We utilize the condition ‘rate< 5000’ in the elif expression. It denotes that the value entered is less than 5000. The line ‘rate is less than 5000’ is presented with the help of the print() method.
Example no 3:
The innermost command will be indented more than the inclusive expression. All commands within a single segment will be extended identically.
price = 2000
quantity = 6
rate = price*quantity
if rate > 1000:
if rate > 1000:
print(«rate is greater than 1000»)
if rate < 1000 and rate > 1500:
elif rate < 1500 and rate > 1300:
print(«rate is between 1300 and 1500»)
print(«rate is between 1200 and 1500»)
elif amount == 2000:
print(«rate is 2000»)
print(«rate is less than 2000»)
At the start of the program, we initialize three variables and give them values. These variables include ‘price’, ‘quantity’, and ‘rate’. The values of variables ‘price’ and ‘quantity’ will be provided by the user but the value of ‘rate’ will be calculated by multiplying the value of price and quantity with each other. We have utilized greater than sign and applied conditions on the value of the ‘rate’ variable. We apply the if-else statement.
Whenever the condition of the ‘if’ statement is fulfilled, the print statement displays the message that ‘Rate is greater than 100’. Otherwise, we use various conditions. Within the portion of the else-condition, we have applied the ‘elif’-expression. The ‘elif’ expression uses the ‘BETWEEN’ and ‘AND’ operators to apply conditions on the value. The ‘elif’ expression shows that ‘rate <1500 and rate > 1300’ then print statement prints the line ‘rate is between 1300 and 1500.
When the specified condition becomes true the print() command of that appropriate condition prints the result. Similarly, outside the body of the if-else statement, we again employ the ‘elif’ statement. We use the equal operator in such a way that ‘rate == 2000’ then print() command shows the line ‘rate is 2000’. Otherwise, it prints the message ‘rate is less than 2000’.
Example no 4:
In this scenario, if, a sequence of ‘elif’ and else will be used to obtain the data type of a specified value.
v = 3558.9769
if (type(v) == int):
print(«Data type of the variable is Integer»)
elif (type(v) == float):
print(«Data type of the variable is Float»)
elif (type(v) == complex):
print(«Data type of the variable is Complex»)
elif (type(v) == bool):
print(«Data type of the variable is Bool»)
elif (type(v) == str):
print(«Data type of the variable is String»)
elif (type(v) == tuple):
print(«Data type of the variable is Tuple»)
elif (type(v) == dict):
print(«Data type of the variable is Dictionaries»)
elif (type(v) == list):
print(«Type of the variable is List»)
print(«Data type of the variable is Unknown»)
Our step is to initialize the variable named ‘v’ and we will provide it with a value. To check its data type, we have been using the ‘if-elif’ statement. The ‘if’ statement checks if the entered value of variable ‘v’ is an integer. Then the print() command prints the line ‘Data type of the variable is Integer’.
After this, the ‘elif’ statement is utilized to see if the defined value is a floating-point value. Then, the print value shows the message related to this. If the value contains the complex portion it means the data type of the variable will be complex so we will use the print() function for this also. Similarly, if the given value contains the ‘True’ or ‘False’, then the data type is Boolean so the print statement shows that the type will be bool.
Then, we use str and tuple to check whether the specified value belongs to the string data type or tuple data type. The ‘elif’ statement is applied to check if the value is related to the dictionary data type or not. In the end, we utilized the equal operator (==) within the ‘elif’ command. This time we have seen if the value is present in the form of a list. Then the print() method prints the line ‘Data type of the variable list’. If the entered value does not belong to any of the above-mentioned data types, then the print() command prints the message ‘Data type of the variable is unknown’.
In this article, we have talked about the ‘elif’ python. The ‘elif’ expression helps to verify several statements for TRUE and run a set of instructions immediately while one of them does. The ‘elif’ expression, like the else command, is unnecessary. Unlike other expressions, that can only have one, the ‘elif’ expression will have an arbitrary value of these after the ‘if’ statement.