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.
]]>So we must use this statement to value if our expression is True or False. For that purpose we’ll use the special word “If”
“If” in python means the same as it does in the english language. So after typing “If” you must state the expression you want to evaluate, either to be TRUE, or FALSE.
An example of this will be.
x = 5
if ( x == 5):
print(‘True’)
It is very important that at the end of our statement we put a colon (:) because this means that the block of code we write next to it (actually under it and with a space tab) will be part of what will be executed in case that our conditional turns out to be TRUE.
In the previous example, the code i wrote translated to english will literally mean:
set the value of x to 5
if x is equal to 5, print ‘True’
But what if our expression is valuated at False. That would mean that our block of code we wrote under the expression wont be executed. In this case we have two options, we can just let it like that or we can arrange a second conditional to say what we want the computer to execute in case that it (the second conditional) turns out TRUE. For this purpose we can use the special word: ELIF, this is kind of a combination of Else If.
So let’s see an example.

In this piece of code we can see how at first instance, the value of x is equal to 5. Then we tried to evaluate x, so we´re saying that if x is equal to 6, print in the screen ‘If’ but because we set the value at 5 this turns out to be false, so it wont print ‘If’
Then we set an Elif, which is kind of “if the last ‘If’ statement doesn´t work, evaluate this to see if it’s true so you can run it.”
The Elif says that if the value of x is equal to 5, print ‘Elif’.
At the moment you run the program, it prints ‘Elif’ because the conditions of Elif is True.
Thats how If and Elif works, you just have make sure you know where and when you want to evaluate
]]>