Else statement in Python


Keywords: if statement, conditional statement, else statement, elif statement

if else statement Python

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.

What is an else statement in Python?

The Else statement in Python is an optional add-on that is used in conjunction with an if statement.

  • 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 on 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

How does the else statement in Python work?

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.

Else statement Python programming

Create else statement in Python

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

  • The else statement is easily declared by the reserved word else
  • Else statement has no condition
  • The else statement is executed when the if condition is false

Syntax: Else statement in Python

if logical expression
   statement doing

else 
   what this statement should perform

Example 1: Else statement in Python

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

Example 2: Else statement in Python

You can imagine that you want to program a lamp.

  • If the light is off and we press the light button, we want to turn on the lamp.
  • On the other hand, if the light is already on, we want to turn off the light – even though we press the same button.
  • Hence, the same button can execute different commands, depending on the current condition we have. In this case, we have two conditions: on or off.

Let’s illustrate the example by using a flow chart

Else statement example Python

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

Why use else statement in Python?

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

Summary: Else statement in Python

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”.

  • The else statement must be used in conjunction with the if statement.
  • The else statement is declared using the reserved word else followed by the code block belonging to the else statement in indent
  • Else statement has no condition that it evaluates and is executed based on the result of the if statements condition
  • For more technical and specific information about the Else statements in Python we recommend the Python docs website

Syntax: Else statement in Python

if logical expression
   statement doing

else 
   what this statement should perform