Elif vs If Else Statement in Python
“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.
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 “return “Equal”.
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:
Then we’ll expect to see a statement that says “Greater” because 10 is greater than 5.
What if we carry out this operation for the number 2?
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.
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.
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.
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?
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
through the elif-statements in the middle
to the “else”- statement at the end.
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 “else” statements and will proceed with the rest of the code.
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”.
Again, if not, we will get to statement number 3 and so on until the computer finds a satisfactory outcome to print out.
The Order of Instructions Matters
Now, we can switch the order of the two elif statements to prove that the order of instructions matters.
Let’s see what happens when we print compare_to_five(-3).
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.
Given we have other statements, it moves forward.
- So, is y less than 5? Yes.
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.
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.
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.