The ELSE Command
The IF construct can be enriched through the use of the ELSE command.
In the previous section we saw how if a condition is met the instructions inside are executed otherwise no. But sometimes we could decide in a construct to execute some instructions if the condition is met, and other instructions if it is not.
if condition:
instruction A
else:
instruction B
This can be very useful in many cases. For example the code of the previous example (that of the even or odd numbers) can be modified with the ELSE command.
s = input("Insert a value:")
a = int(s)
if a%2 == 0:
print("You inserted a pair number")
else:
print("You inserted an odd number")
You can to do this since you are sure that if a number is not even, it is certainly odd. The previous construct has two advantages:
- more orderly flow management
- more efficient execution (one less condition is evaluated)
So the IF-ELSE construct is used to differentiate the operations to be performed when in a condition we want to cover 100% of the cases.
Nested IF-ELSE
Furthermore with IF-ELSE we divide the choice of the possible cases to two groups. But when should we divide the choice into a group greater than 2?
For example, you want to know if a variable is less than zero, between 0 and 10 or greater than 10.
In this case you can use the IF-ELSE construct several times, one nested to the other.
First we will see if the variable is greater than or less than zero.
s = input("Insert a value:")
a = int(s)
if a >= 0:
print("You inserted a value greater or ugual than zero")
else:
print("You inserted a value lower than zero")
This will be the most external IF-ELSE construct. So in simple words we divided the case into two subgroups, the first that includes numbers greater than or equal to zero, the other numbers less than zero. But now we need to know even if it is between 0 and 5 or it is greater than 5. To do this we have to insert an IF-ELSE construct in the first sub-group in order to divide it further.
s = input("Insert a value:")
a = int(s)
if a >= 0:
if a > 5:
print("You inserted a value greater than 5")
else:
print("You inserted a value between 0 and 5")
else:
print("You inserted a value lower than zero")
To have a mental picture, it is useful to write down the following code:
- in how many cases we have to divide the choice,
- think about what conditions to apply,
- in which sequence
- how they must be nested
And in the end you must have in mind a scheme that can be represented as follows:
Needless to say, it is quite equivalent if we split the code in this other way (by nesting the IF-ELSE construct on ELSE instead of on IF).
s = input("Insert a value:")
a = int(s)
if a < 0:
print("You inserted a value lower than 0")
else:
if a > 5:
print("You inserted a value greater than 5")
else:
print("You inserted a value between 0 and 5")
The ELIF Command
We have seen that by nesting the IF-ELSE constructs inside each other, the structure of the code is gradually becoming more and more branching out. In Python there is an alternative way to work with these more complex decision-making structures, simplifying them with the ELIF command, which is none other than the acronym of ELSE-IF.
Applying it to the previous case we have:
s = input("Insert a value:")
a = int(s)
if a < 0:
print("You inserted a value lower than 0")
elif a > 5:
print("You inserted a value greater than 5")
else:
print("You inserted a value between 0 and 5")
As you can see, using this command the code structure is more readable, with the disappearance of progressive indentations, which all remain at the same level.
⇐ Go to Python Lesson 2.2 – The conditional construct IF
Go to Python Lesson 2.4 – The Boolean Logic ⇒