Updated on 15 Jul 2021

# Python ELIF Statement Exercises

Martin Ganchev Published on 12 Nov 2018 2 min read

ELIF Statement Exercise

Given the 3 sides of a triangle - x, y and z - determine whether the triangle is equilateral, isosceles or obtuse.

Note: Equilateral means all sides are equal, isosceles means two of the sides are equal but not the third one, obtuse means all 3 are different.

(Bonus question: Now include a check at the beginning to see whether the lengths of the sides satisfy the triangle inequality. The inequality states that every side of the triangle is shorter than the sum of the remaining two.)

The tutorial you may need: What is Elif in Python

This one sounds difficult but introducing context to a problem makes it much easier to imagine and solve. So, let’s get to it!

Apart from IF and ELSE statements, we will use the ELIF statement (known as ELSE IF in other programming languages). Furthermore, using ELIF statements allow us to easily separate more than two non-overlapping conditions.

Okay! The way we are going to effectively implement this is by clearly identifying which conditions need to be satisfied for one type of triangle, then use and ELIF to check for a second type, thus leaving ELSE to account for the last type.

## Let us start by checking if the triangle is equilateral.

Since equality is transitive, as long as x equals y and x equals z, then z equals y and the triangle is equilateral. Therefore, our first IF statement would only check two conditions and look as follows:

if x==y and x==z:

print ("Equilateral")

Now, using an ELIF statement afterwards means we only have to deal with obtuse or isosceles triangles. This is because the computer checks the conditional statements chronologically. Not satisfying the initial IF statements guarantees the triangle is not equilateral when it reaches the ELIF statement.

Thus, if any two sides are equal we are dealing with an isosceles triangle. Therefore, the condition of the ELIF statement simply needs to check if one among x equals y, y equals z or z equals x is true. This would look as follows:

elif x==y or x==z or y==z:

print ("Isosceles")

This means that any triangles which did not satisfy either condition will have to be obtuse. Therefore, we add the ELSE statement and the print function that follows it.

else:

print("Obtuse")

Putting it all together, the entire code would look like this:

if x==y and x==z:

print ("Equilateral")

elif x==y or x==z or y==z:

print ("Isosceles")

else:

print("Obtuse")

And that is all there is to it. Bear in mind, that we could have chosen the IF and ELIF statements to account for types of triangles other than the ones we did in this approach and the outcome would have been identical. Keep in mind that if we decide to test for isosceles triangles first, we need to explicitly state the third side being of different length.

## And here's the bonus part of this ELIF statement exercise.

We would need to test if the triangle inequality holds true first, so we include an IF statement before our initial IF statement. For the sake of accuracy, we should even change the equilateral testing IF statement to an ELIF. Doing so means that the computer would check for the various types of triangles only if the triangle inequality does not hold true.

Therefore, we need an IF statement to check if the inequality is violated. For this to happen, one of the following 3 conditions needs to be satisfied. Either x is greater than the sum of y and z, y is greater than the sum of x and z or z is greater than the sum of x and y.

Therefore, our IF statement would be as follows:

if x>=y+z or y>= x+z or z>= x+y:

print ("The triangle does not exist")

The way we have set this up means that if a triangle does not exist (does not satisfy the inequality), then we would not even bother checking what type it is. Remember that we changed the equilateral IF statement to and ELIF statement, so the entirety of the new code would look like this:

if x>=y+z or y>= x+z or z>= x+y:

print("The triangle does not exist")

elif x==y and x==z:

print ("Equilateral")

elif x==y or x==z or y==z:

print ("Isosceles")

else:

print("Obtuse")

I know some of this sounds very complex, but once you break it down into smaller increments the task is not too difficult to complete.

Keep up the good work! And if you want to try integrating an IF into a function… then click here.

## Now, let's 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?

Ok. Perfect.

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.

Ha!

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 365 Data Science Training. It covers everything you need from Mathematics and Statistics, through R, Python, and SQL, to Machine learning and Deep learning (with TensorFlow).