I recently posted on how to use IF and ELIF loops. Well, this time im going to show you how to use “ELSE” which is kind of the family of IF and ELIF.

When we use IF we evaluate a condition, if there the chance that it might be FALSE we can add an ELIF to tell the program what to do in case there are two possible options and the first is false. When we have more than two options we can use IF and ELIF, but when we have three options we have to look at our program and see it the third option is the last and unique option left. Let’s see an example.

x = 15

if ( x > 10 ):

print(“greater than 10”)

elif (x>0):

print(“greater than 0 and smaller than 10”)

else:

print(“the number is negative”)

This program will display the message: greater than 10. In case we change the value of x to 2 it will show: greater than 0 and smaller than 10 the number is negative. And the last choice is that if none of that happened it will show: the number is negative. We could have use elif, but instead of that with ELSE we save us some time, some effort make everything have concordance, we save some letters that means the program will be faster (perhaps we don’t see the difference in this case, but we could notice in a larger, complex code).

This is why we should use ELSE, is not better who writes a larger complex program, but who writes a smaller, simple, powerful one.

CC BY 4.0 How to use ELSE by fersabal is licensed under a Creative Commons Attribution 4.0 International License.