What is Elif in Python?

Python Tutorials 6 min read
elif in python thumbnail

What is Elif in Python?

6 min read
Blog / Python Tutorials / What is Elif 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.

two way arrow signpost - elif in python

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.

def compare to five

Using 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:

print compare to five

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

greater

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

less

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

data-science-training

More Elif Statements

Know that you can add as many elifstatements 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.

negative

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.

return less

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.

Control Flow in Python

control flow 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 chart conditional statement

through the elif-statements in the middle

through the elif code

to the “else”- statement at the end.

control flow to the else code

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.

if y is greater than 5

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”.

if y is less than 0

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

if y is less than 5

The Order of Instructions Matters

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

put less before negative

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

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.

disregard later

Given we have other statements, it moves forward.

  • So, is y less than 5? Yes.

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

output 2 = 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 less

Reasons to 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, check out our super practical tutorials!

For an exercise to test your skills try your hand at this: ELIF statement exercise

Leave a Reply

Your email address will not be published.

A Free Data Science Career Guide?

Check your email shortly!