We have the answers to your questions! - Don't miss our next open house about the data universe!

ELIF Python: What you need to know about this function

- Reading Time: 3 minutes
The ELIF function in Python: What you need to know

Testing conditional expressions is the very basis of programming. While the if then else structure is inherent to most languages, Python manipulates another form of condition: if elif else... What is it exactly?

If you’ve ever learned to write a computer program, and whatever your level, you’ll know that an essential element of almost any block of code is the conditional structure.

How does it work?

A particular value (temperature, current date…) is subjected to a test, using comparison operators. An agricultural application might compare the humidity level to a value and trigger watering if it falls below a certain level.

In almost all programming languages, the common code block is based on the if conditional expression.

if humidity_level < low_humidity:

water

If the condition is true (as soon as the humidity level falls below the value specified in the variable low_humidity), then the application triggers watering. If the condition is false, nothing happens.

La structure if then else

Programming languages are usually based on a fundamental structure: if (condition) then (action) else (other action).

if day_of_week == “saturday”:

promotion = price * 0.8

else:

promotion = price * 0.9

In this example, promotional items are sold at 9/10ths of their price from Monday to Friday, and at 8/10ths of their price on Saturday.

In many languages, it’s the combination of the else and if clauses that enables us to test a whole range of conditions. So we have this form:

if (condition):

action 1

else if (condition 2):

action 2

else if (condition 3):

action 3

(…)

The if elif else de PythonPython's if elif else structure

Python’s syntax has introduced another, much clearer and easier-to-use conditional expression: elif. elif can also be used to test a whole series of conditions one after the other, whereas else only comes into play if none of the previous conditions has been met.

If the first condition specified by if is not satisfied,

  • then a 2nd condition specified by elif is tested,
  • potentially a 3rd condition specified by elif,
  • potentially a 4th condition also specified with elif,
  • potentially more conditions specified with elif.
  • In the end, a single else expression takes into account all cases not covered by previous conditional expressions.

As we can see, the if elif else structure proceeds by elimination. One condition is tested, then the next, then the next…

It therefore takes this form:

if (condition):

action 1

elif (condition 2):

action 2

elif (condition 3):

action 3

(…)

else (if none of the above conditions are met):

action

An example of elif usage

Let’s imagine a rock concert for which the organizer has established a price range. Ticket prices depend on the age of the spectator. Young and very young people, as well as those close to retirement age, benefit from low prices. Others have to pay the full price.

We could have a code block like this:

if age < 10:

prix_de_la_place = 8

elif age < 18:

prix_de_la_place = 20

elif age > 60:

prix_de_la_place = 15

else:

seat_price = 35

4 spectators come to the ticket office. Arnold is 5 years old and his case is covered by the condition specified by if. Tancrède is 17 and his situation is covered by the first elif condition. John is 62, and will pay 15 euros in accordance with the second elif condition. Finally, Jane, who is 34, falls into the other cases specified here with else, and will therefore have to pay the full price.

The advantages of using Python

Here, as in other situations, Python offers us an easy-to-use tool that is very clear and makes it easy to debug a block of code involving conditional expressions. Python’s popularity is due to its ability to write clear instructions that are easy to read and decipher, and the if elif else structure is an excellent example of this. It’s easy to see why so many developers have come to prefer Python to Java…

You are not available?

Leave us your e-mail, so that we can send you your new articles when they are published!
icon newsletter

DataNews

Get monthly insider insights from experts directly in your mailbox