[vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Else statement in Python

[/vc_column_text][vc_separator type=”normal” up=”1″][vc_column_text css=”.vc_custom_1570780001573{background-color: #f6f6f6 !important;}”]

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

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

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. 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.[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

The Else statement specifies what will happen in the program when the condition of an if statement is not met

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]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.[/vc_column_text][vc_empty_space height=”20px”][vc_row_inner row_type=”row” type=”full_width” text_align=”left” css_animation=””][vc_column_inner][vc_single_image image=”19426″ img_size=”medium” alignment=”center” onclick=”link_image” qode_css_animation=””][/vc_column_inner][/vc_row_inner][vc_empty_space height=”20px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_column_text]

CREATE A ELSE STATEMENT IN Python – SYNTAX

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

 

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

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_column_text]

Example of the Else statement in Python

Below is a short and simple example that clearly shows how the Else statement complements the If statement.[/vc_column_text][vc_empty_space height=”10px”][vc_column_text]

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

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_column_text]

Additional example of the Else statement in Python

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.[/vc_column_text][vc_empty_space height=”10px”][vc_row_inner row_type=”row” type=”full_width” text_align=”left” css_animation=””][vc_column_inner][vc_single_image image=”19433″ img_size=”medium” alignment=”center” onclick=”img_link_large” img_link_target=”_blank” qode_css_animation=””][/vc_column_inner][/vc_row_inner][vc_empty_space height=”20px”][vc_column_text]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

[/vc_column_text][vc_empty_space height=”30px”][vc_column_text]

For more technical and specific information about the Else statements in Python we recommend the Python docs website

[/vc_column_text][vc_empty_space height=”20px”][vc_separator type=”normal”][vc_empty_space height=”20px”][/vc_column][/vc_row][vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Previous Page    |    Next Page

[/vc_column_text][vc_empty_space][/vc_column][/vc_row]