The Else statement in Python is used when you want to execute a command when none of the conditions in the other if statements are fulfilled (true).
The Else statement in Python is an optional add-on that is used in conjunction with an if statement. The Else statement specifies what should happen in the program when the condition of an if statement is not met, in other words, when the if condition is false. The if statement tells which operations to perform when the if statement is true. Without the other statement, the program goes directly after the if statement. However, if an else statement is specified, all operations within the else statement will be performed first. Similarly, if the condition in the if statement is met (true), then the operations in the if statement will be performed as usual, but the operations in the else statement will not be executed.
The Else statement specifies what will happen in the program when the condition of an if statement is not met
Furthermore, the flowchart below shows how the if condition determines if the if statement operations are to be performed if the if condition is true or if the else operations are to be performed if the if condition is not met.
The if statement is specified as usual, followed by the else statement created with the reserved word else. Note that the else statement has no logical expression, since the else statement will always be executed if the logical expression in the if statement is false. Thus, it is the if statement that controls whether or not the else statement is to be executed
if logical expression statement doing else what this statement supposed to do
Below is a short and simple example that clearly shows how the Else statement complements the If statement.
int number = 5; if number > 6: print("This is the IF statement") else print("This is the ELSE statement")
Since the variable number is not greater than the value 6 (the condition for the if statement), the printout will be:
This is the ELSE statement
Imagine you want to program a lamp. If the lamp is off, and we press the lamp button, the lamp will light.
If the lamp is on, the lamp will turn off – even though we press the same button! The same button can therefore execute different commands, depending on the condition we have. In this case, we have two conditions. On, or off.
We have already encountered the boolean data type, which has the value false or true. Let’s create a variable that keeps track of whether the lamp is on or off.
isLampOn = False # isLampOn is declared as false, the light is OFF
What we want to happen if the lamp is off is for it to turn on – and light. Or if the lamp is on, we want it to turn off. After all, the lamp cannot be lit if it is already lit or turn of the lamp when it is already off. Please note the following program code:
isLampOn = False if (isLampOn): isLampOn = False; print("The lamp is turned OFF") else isLampOn = True; print("The lamp is turned ON")
The result in this case is
The lamp is turned ON