Match Case Statement in Python

Match Case Statement

A Match Case statement contains an expression and multiple case blocks. Each case block has a value or pattern to be matched with the expression. Match statement takes the expression and compares it with the values in the case blocks, from top to bottom. When the expression’s value matches with the case value, respective block is executed.

The syntax of a typical match statement is

match expression:
  case value1:
    #do something
  case value2:
    #do something

We can have a default case block, where none of the case values matches the expression. In the following, the last case block is the default block.

match expression:
  case value1:
    #do something
  case value2:
    #do something
  case _:
    #do something default

Note: Python Match statement is introduced in Python version 3.10. Python version 3.10 or above must be present to use Match statement in your program.

Examples

A Simple Example for Match Statement

In the following example, we take a variable for expression, and match it against string values in case blocks.

Python Program

x = input('Enter a fruit name : ')
match x:
    case "apple":
        print("Its red in color.")
    case "banana":
        print("Its yellow in color.")
    case "orange":
        print("Its orange in color.")

Output#1

Enter a fruit name : banana
Its yellow in color.

Output#2

Enter a fruit name : apple
Its red in color.

Output#3

Enter a fruit name : guava

Match Statement with Default Case

In the previous example, we do not have a default case block. So, if user enters a fruit name that does not match with any case block, then no case block is run.

But, if we would like to run some default code when there is no match with the case values, we include a default case block, as shown in the following program.

Python Program

x = input('Enter a fruit name : ')
match x:
    case "apple":
        print("Its red in color.")
    case "banana":
        print("Its yellow in color.")
    case "orange":
        print("Its orange in color.")
    case _:
        print("This fruit is not present in our shop.")

Output#1

Enter a fruit name : grape
This fruit is not present in our shop.

Output#2

Enter a fruit name : apple
Its red in color.

Match Statement with a Case containing Multiple Values

In our previous examples, we have seen case blocks matching a single value. We can have a case block, where more than one value is considered for matching with the expression’s value, using | operator.

In the following example, in the first case block, we are having two values.

Python Program

x = input('Enter a fruit name : ')
match x:
    case "apple" | "cherry":
        print("Its red in color.")
    case "banana":
        print("Its yellow in color.")
    case "orange":
        print("Its orange in color.")
    case _:
        print("This fruit is not present in our shop.")

Output#1

Enter a fruit name : cherry
Its red in color.

Output#2

Enter a fruit name : apple
Its red in color.

Output#3

Enter a fruit name : banana
Its yellow in color.

Summary

In this tutorial of Python Examples, we learned about Match statement in Python, and how to use it, with detailed examples.