Python ‘else if’ Is Called ‘elif’

In Python, an else if block is called elif.

For instance:

temp = 100

if temp <= 32:
    print("It is freezing cold")
elif 32 < temp < 70:
    print("It is cold")
else:
    print("It is warm")

Let’s take a deeper look at ifelifelse statements in Python.

Python If-Elif-Else Statements

In Python, if statements are used to check conditions. You can take specific actions based on the outcome.

In Python, use the ifelifelse structure to check if conditions hold.

Let’s start with the simplest version—the if statement.

If Statements in Python

You can specify an if statement to check if a criterion is met.

For example, you can check if a number is greater than another, and react to it accordingly.

For instance:

age = 20

if age >= 18:
    print("You are old enough to drive")

Output:

You are old enough to drive

It is fine to use an if statement without elif or else.

Next, let’s broaden the example by adding an elif statement to handle the ages of 17.

Elif Statement—Python’s Version of Else If

In Python, the else if statement is called elif. This is different compared to other programming languages. Usually, you see else if or elseif.

The elif means “if the previous condition(s) was not true, try this”.

An elif statement cannot be used as a standalone. It must be preceded by an if statement.

However, there can be as many elif statements as you need.

For example, let’s add a condition for 17 year-olds:

age = 17

if age >= 18:
    print("You are old enough to drive")
elif age == 17:
    print("You can join to driving school")

Output:

You can join to driving school

The Else Statement in Python

The else statement “catches” anything that was not caught by the if or elif statements.

An else statement cannot be used as a standalone. It must be preceded by an if statement.

In the above example, we have defined what happens when the age is either 17 or 18 and above. But if the age is below 17, nothing happens. This does not mean you have to define what happens. But in this context it makes sense.

So let’s add an else statement to handle ages below 17:

age = 9

if age >= 18:
    print("You are old enough to drive")
elif age == 17:
    print("You can join to driving school")
else:
    print("You are too young for driving")

Output:

You are too young for driving

In the above:

  • The if statement handles ages of 18 and over.
  • The elif statement handles ages equal to 17.
  • The else statement handles all the ages that do not belong to the two above. In other words, all the ages less than 17. (Also negative ages)

Multiple Elif Statements in Python

Finally, let’s demonstrate how you can have multiple elif statements in the same if…elif…else statement.

For example, in the previous example is you can have a negative age. That should not be possible. To solve this, we need to have four different cases:

  1. Ages of 18 and over.
  2. Ages of 17.
  3. Ages between 0 and 16.
  4. Ages below 0. These should trigger an error.

So we need one additional elif check to do this. Let’s turn this into code:

age = 9

if age >= 18:
    print("You are old enough to drive")
elif age == 17:
    print("You can join to driving school")
elif 0 <= age <= 16:
    print("You are too young for driving")
else:
    print("Negative age is not possible!")

Now, this piece of code takes into account all the possible age cases.

The main point is that there is nothing wrong with having multiple elif statements.

Conclusion

Python elseif is called elif. This is different from other programming languages as they commonly use elseif or else if.

For example:

temp = 100

if temp <= 32:
    print("It is freezing cold")
elif 32 < temp < 70:
    print("It is cold")
else:
    print("It is warm")

Thanks for reading. I hope you enjoy it.

Happy coding!

Further Reading

50 Python Interview Questions and Answers

50+ Buzzwords of Web Development