Python Elseif Is Called Elif

In Python, elseif block is created with 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 if-elif-else 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 if-elif-else 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 ages of 17.

Elif Statement—Python’s Version of Else If

In Python, the else if statement is called elif. This is different comparing 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 cannot be used as standalone. It must be preceded by an if statement.

There can be as many elif statements structure 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 standalone. It must be preceded by an if-check.

n 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 construct.

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 here is nothing wrong with having multiple elif statements.

Conclusion

Python’s elseif is marked with elif. This is different to 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

Share on facebook
Facebook
Share on google
Google+
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest

Leave a Comment

Your email address will not be published. Required fields are marked *