Register Login

What is elif in Python?

In our life we often encounters situation where we need to take decisions from among multiple conditions. In programming also, we can perform such multiple decision-making code using if-elif statements.

In this chapter, we will learn about the elif statement in Python. Also, we will see how and where we can use this statement. This statement helps to check whether the condition is true or false and then work accordingly.

How Elif (else + if) Statement Works?

For verifying multiple expressions, we can use this statement. Let us consider that the condition for 'if' is false, then it automatically checks for the other 'elif' block. The program automatically evaluates and executes the remaining statements if the given test expression is true. We can use the if statement without using the elif and else.

However, we cannot use the else and elif statement without using else.

Syntax:

if(condition(s)):
   statement(s)
elif(condition(s)):
    statement(s)
else:
   statement(s)

Code:

num = 1122
if 9 < num < 99:
     print("Two-digit number")
elif 99 < num < 999:
     print("Three-digit number")
elif 999 < num < 9999:
     print("Four digit number")
else:
     print("number is <= 9 or >= 9999")

Output:

Difference between if-else and elif

We can combine an if with an else statement. With the help of elif statements we can check multiple conditions & if the multiple expressions are TRUE.
According to the 'if' statement, only one 'else' can be there.

As per the if statement, more than
one elif statement can be there.

If the condition is false, the else statement will be executed. In case of multiple conditions, the elif statement will be executed.

Code:

a = 10
if(a < 3) :
      print("a is less than 3")
elif(a != 10) :
       print("a is not equal to 10")
elif(a > 8) :
     print("a is greater than 8")
else:
      print("a is zero")

Output:

We can include as many elif statements as we want to without any strict limit, overlooking the possible limitations such as hardware. We can say that the elif statement is a resolved version of the Nested if-else. In case of more than one statement, we can put many elif conditions between the else and if.

In the following example, you will have to use 2 different variables – room and area. Here, we will check different rooms and spaces using the if elif and else statements followed by if else for checking the size or area of the room.

Code:

#defining variables
room = "bed"
area = 14.0
# constructing a room checking module with if elif else statements
if room == "bath":
     print("Want to look around space for the bathroom.")
elif room == "bed":
     print("Want to look around in the bedroom.")
else :
      print("Want to look around elsewhere.")
# constructing a size checking code using if else
if area > 15 :
      print("\n The place is big!")
else :
       print("\n The place is pretty small.")

Output:

In general, elif means else with if with an opportunity to place a conditional expression. In other programming languages, we use else if(), else-if, or elseif for the same purpose. Python compacts these statements to one single word elif. Python also supports the nested elif statement. The nested statement means to put elif-else inside if-elif-else statements.

Conclusion

We all encounter multiple conditions in different situations. The same goes with programming also. This article catered to the facts and use of elif statement in Python, how it works, and how we can use it with if. The differences between an elif statement and an else statement are simple. We can combine the else with if. On the other hand, we use elif statements to check if the multiple expressions used are TRUE.