Branching

1. Introduction

Branching controls the flow of a program. This part considers a couple of ways of branching in Python. The main way uses an 'if' statement.

2. If

The if statement is a 'compound' statement (one that comprises groups of other statements) that provides a means to branch based upon a condition which evaluates to either 'True' or 'False'. Consider the following example:

day_of_week = 5
day = "Weekday"
# A simple If Statement
if day_of_week >= 6:
    day = "Weekend"
print(day)

The if statement condition evaluates as False, so the result is:

Weekday

Changing the code slightly to:

day_of_week = 6
# A simple If Statement
day = "Weekday"
if day_of_week >= 6:
    day = "Weekend"
print(day)

Results in:

Weekend

An 'else' clause branches into two distinct paths which become one again at the end of the compound if statement as in the following example.

day_of_week = 5
# An If-else Statement
if day_of_week < 6:
    day = "Weekday"
else:
    day = "Weekend"
print(day) # <-- Prints Weekday

One or many 'elif' clauses can also be inserted between if and else clauses. Elif is short for 'else if'. Consider the following example:

day_of_week = 5
# An If-elif-else Statement
if day_of_week == 1:
    day = "Monday"
elif day_of_week == 2:
    day = "Tuesday"
elif day_of_week == 3:
    day = "Wednesday"
elif day_of_week == 4:
    day = "Thursday"
elif day_of_week == 5:
    day = "Friday"
else:
    day = "Weekend"
print(day) # <-- Prints Friday

It can be argued that it is better to store a dictionary to look up the day from the day_of_week, but a simple look up does not branch, and more code can be inserted in any of the clauses, so this can do more than just look up a value from a key.

3. Match

Since Python 3.10 there is also a match statement which can simplify if statements with many elif clauses as a 'match-case' statement. The following example shows the equivalent of the example from the end of previous section:

day_of_week = 5
match day_of_week:
    case 1:
        day = "Monday"
    case 2:
        day = "Tuesday"
    case 3:
        day = "Wednesday"
    case 4:
        day = "Thursday"
    case 5:
        day = "Friday"
    case _:
        day = "Weekend"
print(day) # <-- Prints Friday

The final case is a catch all case using the anonymous variable '_' which matches anything.

The match-case statement offers more than a syntactic variation, as containers and other objects can be matched. Examples of these and further details can be found in the relevant PEPS: