Free Python course. Sign Up for tracking progress →

Python: Match Operator

Many programming languages, in addition to the if conditional statement, include a switch construct. With the release of Python 3.10, a similar functionality was introduced called the match operator. In this lesson, we will explore this operator.

The match operator is a specialized version of if, designed for specific situations. For instance, it is useful when dealing with a chain of if else statements that check for equality:

if status == 'processing':
    # Do something for 'processing'
elif status == 'paid':
    # Do something for 'paid'
elif status == 'new':
    # Do something for 'new'
else:
    # Do something for everything else

The distinguishing feature of this composite check is that each branch corresponds to a check on the variable status. The match operator allows us to express this code more succinctly:

match status:
    case 'processing':  # status == 'processing'
        # Do something for 'processing'
    case 'paid':  # status == 'paid'
        # Do something for 'paid'
    case 'new':  # status == 'new'
        # Do something for 'new'
    case _:  # else
        # Do something for everything else

In terms of elements, the match operator is a complex construct. It consists of the following:

  • The outer description, which includes the keyword match. This represents the variable whose values will determine the behavior chosen by match.
  • case constructs inside, where we describe the behavior for different values of the considered variable. Each case corresponds to an if in the example above. The case _ is a special situation that corresponds to the else branch in conditional statements. Specifying case _ is optional, similar to using else.

Inside match, only the syntax shown above is permitted. In other words, we can use case. However, inside each case, the situation is different. Here, we can execute any arbitrary code:

match count:
    case 1:
        # Do something useful
    case 2:
        # Do something useful
    case _:
        # Do something

Sometimes, the result obtained inside a case leads to the end of the function containing the match. In such cases, it needs to be returned somehow. There are two ways to handle this:

The first approach involves creating a variable before the match, filling it in case, and then returning the value of that variable at the end:

def count_items(count):
    # Declare a variable
    result = ''

    # Fill it
    match count:
        case 1:
            result = 'one'
        case 2:
            result = 'two'
        case _:
            result = None

    # Return it
    return result

The second and simpler approach is to directly return from the function while working with case:

def count_items(count):
    match count:
        case 1:
            return 'one'
        case 2:
            return 'two'
        case _:
            return None

While the match operator is not strictly necessary in Python, it offers the advantage of better expressing the programmer's intent when checking specific variable values. Though the code may grow slightly in size, it becomes more readable compared to using elif blocks.

Instructions

Implement the function get_number_explanation(), which takes a number as input and returns an explanation for that number. If there is no explanation for the number, return just a number. Explanations are available only for the following numbers:

  • 666 - devil number
  • 42 - answer for everything
  • 7 - prime number

Function call examples:

get_number_explanation(8)  # just a number
get_number_explanation(666)  # devil number
get_number_explanation(42)  # answer for everything
get_number_explanation(7)  # prime number

Tips