What is Elif in Python?

Join over 2 million students who advanced their careers with 365 Data Science. Learn from instructors who have worked at Meta, Spotify, Google, IKEA, Netflix, and Coca-Cola and master Python, SQL, Excel, machine learning, data analysis, AI fundamentals, and more.

Start for Free
Martin Ganchev Martin Ganchev 21 Apr 2023 6 min read

Elif vs If Else Statement in Python

Despite having the same purpose as “else if” in many other programming languages, elif in Python is 1 word and 3 characters less so you can code faster :)

“It is not hard to make decisions when you know what your values are.” – Roy E. Disney

This saying is quite relatable when talking about programming in Python. If you know how to use the “if-else” statement in Python, you may easily expand your knowledge by finding out what elif in Python is.

In this tutorial, we’ll learn an elegant way of adding a second “if” statement to one of our expressions. This is done with the help of the elif keyword. In the next picture, we are using it in a function which compares a given number to 5.

Using the elif keyword in a function which compares a given number to 5

How to Use Elif in Python?

Looking at the picture above, if y is not greater than 5, the computer will think: “else if y is less than 5” and it will print out “Less”. And the “else”-statement follows as a tail with the respective block that says “returnEqual”.

Let’s confirm we wrote the code correctly.

We can print out the compare_to_five() function with a value of y equal to 10 in the following way:

Printing out the compare_to_five() function with a value of y equal to 10

Then we’ll expect to see a statement that says “Greater” because 10 is greater than 5.

Greater statement

What if we carry out this operation for the number 2?

Less statement

To obtain the third outcome, we must compare the number 5 with a number that is not greater or smaller than 5. This will happen only if the argument of the function is 5.

Equal statement  

More Elif Statements

Know that you can add as many elif- statements as you need. Let’s provide an example. If y is less than 0, the string “Negative” should be displayed. We achieve that by placing this block between the “if and the other elif statement.

If y is less than 0, the string “Negative” should be displayed.

The function with an argument of minus 3 shows “Negative”, just as it should. We can control whether our program will run properly if we ask it to compare_to_five() a value that lies in the range between 0 and 5.

Less statement when we ask the program to compare_to_five() a value that lies in the range between 0 and 5

A very important detail you should try to remember is that the computer always reads your commands from top to bottom. Regardless of the speed at which it works, it executes only one command at a time. Scientifically speaking, the instructions we give to the machine are part of a control flow.

What Is Control Flow in Python?

Control flow in Python: Chart

This is something like the flow of the logical thought of the computer, the way the computer thinks – step by step, executing the steps in a rigid order.

When it works with a conditional statement, the computer’s task will be to execute a specific command once a certain condition has been satisfied. It will read your commands from the “if”-statement at the top

Control flow in Python with conditional statement, from the If statement at the top: Chart

through the elif-statements in the middle

Control flow in Python with conditional statement through the Elif code in the middle: Chart

to the “else”- statement at the end.

Control flow in Python with conditional statement to the Else code: Chart

The first moment the machine finds a satisfied condition, it will print the respective output and will execute no other part of the code from this conditional.

An Example of the Control Flow in our Program

In our example, if the first statement is correct, we will see the corresponding output number 1, which is printing the string "Greater”. The computer will disregard the elif and the “elsestatements and will proceed with the rest of the code.

An example of the control flow in our program: If y is more than 5, the output is Greater

If the first statement is not correct, we will move forward, and the computer will check whether our second statement is true. If yes, we will see output number 2, which is printing the string “Negative”.

An example of the control flow in our program: if y is less than 0, the output is Negative

Again, if not, we will get to statement number 3 and so on until the computer finds a satisfactory outcome to print out.

An example of the control flow in our program: if y is: if y is less than 5, the output is Less

The Order of Instructions Matters

Now, we can switch the order of the two elif statements to prove that the order of instructions matters.

Switching the order of the two elif statements: putting Less before Negative

Let’s see what happens when we print compare_to_five(-3).

Printing compare_to_five(-3): Obtaining output Less

Instead of “Negative”, we obtained “Less”. This is how the computer reasons:

  • assume y equals -3.
  • Is it greater than 5?
  • No, so the computer continues and checks if there are any other statements in our code.

It would completely disregard the line ‘return “Greater”’ as it doesn’t satisfy the condition.

Disregarding the line ‘return “Greater”’ as it doesn’t satisfy the condition if y is more than 5

Given we have other statements, it moves forward.

  • So, is y less than 5? Yes.

Given we have other statements, the computer moves forward: -3 is less than 5.

At this moment, the computer thinks, “Lovely, I got it! My number is less than 5. I satisfy what my programmer asked me to do, I print out ‘Less’ and I am fine”. And the machine stops there and does not execute a single letter of the code that follows in this block.

Once the condition -3 is less than 5 is satisfied, the output is Less

The fact that we examined the cases when y is less than 0 or equal precisely 5 have no application. They become useless. Whether we ask for the output of minus 3 or 3, we will still have to be satisfied with the “Less” label.

Print compare to five with output Less

Why Use Elif in Python?

Because we usually prefer having more than 2 options when choosing something, elif in Python is the tool to accomplish that. Provided that you keep in mind the order in which you declare your commands, you are good to go.

So now that you know how to get the computer to make decisions using elif in Python, you can dive into the world of Python functions. If you’re enthusiastic about boosting your Python knowledge, you can check out our Introduction to Python course.

For an exercise to test your skills, try your hand at this: ELIF statement exercise. And, in case you're looking for more Python-specific resources, you can discover them in our all-comprising Python Programming Guide.

Martin Ganchev

Martin Ganchev

Instructor at 365 Data Science

Martin holds an MSc degree in Economic and Social Sciences from Bocconi University. His diverse academic and research experience combined with his friendly and explanatory approach to teaching have made him one of the most beloved instructors on our team. Some of the courses he has authored include: SQL, SQL + Tableau, SQL+Tableau+Python, Introduction to Python, Introduction to Jupyter, to name a few.

Top