Keywords: if statement, conditional statement, else statement, elif statement
This article will look at the else statement in Python, also referred to as the if-…else statement in Python. An else statement is used in conjunction with the if statement (as we looked at in the previous article) when you want to perform one or more operations when the conditions in the if statement is false.
The Else statement in Python is an optional add-on that is used in conjunction with an if statement.
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 should perform
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
You can imagine that you want to program a lamp.
Let’s illustrate the example by using a flow chart
We will start by declaring a Boolean variable to track whether the lamp is on or off.
# isLampOn is declared as false, the light is OFF isLampOn = False
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
I’m sure you already have got an understanding of why the else statement in Python is so useful. But to be extra clear; what if we want our program to perform an operation when the if statements condition is false, this is where the else statement comes to use. Therefore, we can use the else statement together with the if statement to execute a code block when the If statement’s conditions are false
The else statement in Python is used to specify a block of code to be executed if the condition in the if statement is false. The keywords used for the if statement and else statement describes their relationship quite well. “If this condition is true, perform the associated code block, else, execute this code block”.
if logical expression statement doing else what this statement should perform
Please leave feedback and help us continue to make our site better.
How useful was this article?
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?