Skip to content

Selection

Selection, also called branching, control statements and conditional statements, are the way in which we make a choice between 2 or more possible outcomes in code. Using the lessons in the evaluation section, you can construct 'if' statements.

If

The first control statement you need to learn in Python is the if statement. If statements are used in the construction of conditional execution to create decision making code. The 'if' part will execute if the statement evaluates as true. Consider this example:

example-3.py from 1 to 30
# example-3.py 
# if statements and control statements
# Remember an if statement only triggers if it evaluates to True

variable_1 = 10 
variable_2 = 5 
variable_3 = "trigonometry"
variable_4 = 5.5 
variable_5 = False 

if variable_1 > variable_2:
    print(f"{variable_1} is greater than {variable_2}")

if variable_1 < variable_2:
    print(f"{variable_1} is less than {variable_2}")

if variable_1 == variable_2:
    print(f"variable_1:{variable_1} and variable_2:{variable_2} are equal")

if type(variable_3) == int:
    print(f"variable_3 is an integer not a string, see: {variable_3}")

if type(variable_4) == float:
    print(f"variable_4 is a float type with a value of {variable_4}")

if variable_5 is True:
    print(f"variable_5 is {variable_5}")

if variable_5 is not True:
    print(f"variable_5 is {variable_5}")
Output
10 is greater than 5
variable_4 is a float type with a value of 5.5
variable_5 is False

Looking through this code you can see that some of the statements aren't printed. For instance, variable_3 is not of a type integer, you can also see that there are some statements that are there to catch different values of variable_1 and variable_2, and a statement to catch if variable_5 is True or False. This way of writing code isn't very readable however, because there are no logical connections between one statement and the others. This is where the elif statement comes in.

Elif

We use the 'else if' or 'elif' statement to logically join statements together and run multiple checks on the same objects. If we take the code above and turn it into a more complex set of if statements then we get something a little clearer:

example-4.py from 1 to 30
# example-4.py 
# elif statements and evaluations
# Remember an if or elif statement only triggers if it evaluates to True

variable_1 = 10 
variable_2 = 5 
variable_3 = "trigonometry"
variable_4 = 5.5 
variable_5 = False 

if variable_1 > variable_2:
    print(f"{variable_1} is greater than {variable_2}") 

elif variable_1 < variable_2:
    print(f"{variable_1} is less than {variable_2}")

elif variable_1 == variable_2:
    print(f"variable_1:{variable_1} and variable_2:{variable_2} are equal")

if type(variable_3) == int:
    print(f"variable_3 is an integer not a string, see: {variable_3}")

if type(variable_4) == float:
    print(f"variable_4 is a float type with a value of {variable_4}")

if variable_5 is True:
    print(f"variable_5 is {variable_5}")

elif variable_5 is not True:
    print(f"variable_5 is {variable_5}")
Output
10 is greater than 5
variable_4 is a float type with a value of 5.5
variable_5 is False

As you can see the code is a little more logically grouped and it helps with processing cycles too. Once an if statement is executed, if it evaluates to true in the first part of the statement then it doesn't have to run any of the elif statements. As soon as one of the steps evaluates to true then the evaluation can exit there. In our previous example, each if statement has to be evaluated independently, increasing the amount of processing that needs to be done and making your code less readable. But what happens if you evaluate something and you don't trigger any condition, well there is the 'else' statement that handles that.

Else

The else statement is designed to run after everything in the if/elif statement evaluates to False. Consider it a piece of code that runs 'if all else fails'. Else statements can also be used in loops which you will see later. Let's take the code we had in example 3 and 4 and improve it further to incorporate else statements:

example-5.py from 1 to 40
# example-5.py 
# else statements
# Remember an if or elif statement only triggers if it evaluates to True,
# if all of them evaluate to false then the else statement runs.

variable_1 = 10 
variable_2 = 5 
variable_3 = "trigonometry"
variable_4 = 5.5 
variable_5 = False 

if variable_1 > variable_2:
    print(f"{variable_1} is greater than {variable_2}")        
elif variable_1 < variable_2:
    print(f"{variable_1} is less than {variable_2}")        
elif variable_1 == variable_2:
    print(f"variable_1:{variable_1} and variable_2:{variable_2} are equal")
else:
    print(f"Something went very wrong!! Variable 1 or 2 are broken.")


if type(variable_3) == int:
    print(f"variable_3 is an integer not a string, see: {variable_3}")
else:
    print(f"variable_3 is not an integer, see: {variable_3}")


if type(variable_4) == float:
    print(f"variable_4 is a float type with a value of {variable_4}")
else: 
    print(f"variable_4 is the wrong type.")    


if variable_5 is True:
    print(f"variable_5 is {variable_5}")
elif variable_5 is not True:
    print(f"variable_5 is {variable_5}")
else:
    print(f"variable_5 is not a Boolean for some reason.")    

## This file has some dependencies in the README.md file, check line 50. If you change this file you might have to change it there too.  
Output
10 is greater than 5
variable_3 is not an integer, see: trigonometry
variable_4 is a float type with a value of 5.5
variable_5 is False