Next video: Python Tuples
Else if, for Brief – ELIF
In this lesson, 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, as shown in this example.
If y is not greater than 5, the computer will think: “else if y is less than 5”, written “elif y is less than 5”, then I 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. Correct?
What if we carry out this operation for the number 2? The machine tells us that 2 is less than 5. And that’s what we expected.
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, right? Shall we try this one?
Great! We obtained “Equal”, as expected.
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. I will place this block between the if and the other elif statement.
Let’s see what happens.
The function with an argument of minus 3 shows “negative”, just as it should. Let me just control whether our little program will run properly if I asked it to “compare to five” a value that lies in the range between 0 and 5, say 3. Yes, we see “Less”, so everything is ok.
A very important detail you should try to remember is 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. 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.
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”. If not, we will get to statement number 3 and so on until the computer finds a satisfactory outcome to print out.
Now, I will switch the order of the two elif statements to prove that the order of instructions matters. Ok?
Let me print “compare to five” of minus 3.
Instead of “Negative” we obtained “Less”. This is how the computer reasons: assume y equals -3. Print out “Greater” if y is greater than 5. Is it greater than 5? No, so the computer continues and checks if there are any other statements in our code. Given we have other statements, it moves forward. So, is y less than 5? Yes, it is. 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 you examined the cases when y is less than 0 or equal precisely to 5 have no application. They become useless. Whether you ask for the output of minus 3 or 3, you will still have to be satisfied with the “Less” label.
You found this interesting, didn’t you? For more useful lessons, check out our Python for Finance: Investment Fundamentals and Data Analytics course here.