Skip to content

Match Case Statement in Python

Mastering Match Case Statement in Python: A Comprehensive Guide

Match case statement in Python is used to control the flow of execution of the program. It is also known as pattern matching statement. Match case statement is new in Python 3.10. In this comprehensive guide, we’ll explore the syntax and usage of match case statement in Python.

What is Match Case Statement in Python?

Match case statement in Python is used to control the flow of execution of the program. It is also known as pattern matching statement. Match case statement is new in Python 3.10. It is similar to switch case statement in other programming languages. It is used to compare the value of an expression against a list of patterns and execute the corresponding block of code if a match is found.

Syntax of Match Case Statement in Python

The syntax of match case statement in Python is as follows:

Syntax
match expression:
    case pattern1:
        # statements
    case pattern2:
        # statements
    case pattern3:
        # statements
    ...
    case patternN:
        # statements
    case _:
        # statements
Syntax
match expression:
    case pattern1:
        # statements
    case pattern2:
        # statements
    case pattern3:
        # statements
    ...
    case patternN:
        # statements
    case _:
        # statements

Diagram:

Diagram of the Match Case Statement in Python


  graph TD
    A[Start] --> B{expression}
    B -- pattern1 --> C[statements]
    B -- pattern2 --> D[statements]
    B -- pattern3 --> E[statements]
    B -- patternN --> F[statements]
    B -- _ --> G[statements]
    C --> H[End]
    D --> H
    E --> H
    F --> H
    G --> H

Match Case Statement

This syntax is equivalent to the c++, Java, and JavaScript switch case statement.

The match case statement starts with the matchmatch keyword followed by an expression. The expression is evaluated and compared against the patterns. If a match is found, the corresponding block of code is executed. If no match is found, the block of code under the __ pattern is executed. The __ pattern is known as the wildcard pattern. It matches any value.

Example of Match Case Statement in Python

The following example demonstrates how to use the match case statement in Python:

match_case.py
# match case statement
x = 4
match x:
    case 1:
        print("x is 1")
    case 2:
        print("x is 2")
    case 3:
        print("x is 3")
    case 4:
        print("x is 4")
    case 5:
        print("x is 5")
    case _:
        print("x is not 1, 2, 3, 4, or 5")
match_case.py
# match case statement
x = 4
match x:
    case 1:
        print("x is 1")
    case 2:
        print("x is 2")
    case 3:
        print("x is 3")
    case 4:
        print("x is 4")
    case 5:
        print("x is 5")
    case _:
        print("x is not 1, 2, 3, 4, or 5")

Output:

command
C:\Users\Your Name> python match_case.py
x is 4
command
C:\Users\Your Name> python match_case.py
x is 4

In the above example, we have used the match case statement to compare the value of the variable xx against a list of patterns. Since the value of xx is 44, the block of code under the case 4:case 4: is executed.

Another example of match case statement in Python:

match_case.py
# match case statement
x = 10
match x:
    case 1:
        print("x is 1")
    case 2:
        print("x is 2")
    case 3:
        print("x is 3")
    case 4:
        print("x is 4")
    case 5:
        print("x is 5")
    case _:
        print("x is not 1, 2, 3, 4, or 5")
match_case.py
# match case statement
x = 10
match x:
    case 1:
        print("x is 1")
    case 2:
        print("x is 2")
    case 3:
        print("x is 3")
    case 4:
        print("x is 4")
    case 5:
        print("x is 5")
    case _:
        print("x is not 1, 2, 3, 4, or 5")

Output:

command
C:\Users\Your Name> python match_case.py
x is not 1, 2, 3, 4, or 5
command
C:\Users\Your Name> python match_case.py
x is not 1, 2, 3, 4, or 5

In the above example, we have used the match case statement to compare the value of the variable xx against a list of patterns. Since the value of xx is not 11, 22, 33, 44, or 55, the block of code under the case _:case _: is executed.

Combining Match Case Statement

The match case statement can be combined with other control statements such as if-else statement, for statement, while statement, etc. The following example demonstrates how to combine the match case statement with the if-else statement:

match_case.py
# match case statement
day = "Monday"
match day:
    case "Saturday" | "Sunday":
        print("It's a weekend")
    case "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday":
        print("It's a weekday")
    case _:
        print("Invalid day")
match_case.py
# match case statement
day = "Monday"
match day:
    case "Saturday" | "Sunday":
        print("It's a weekend")
    case "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday":
        print("It's a weekday")
    case _:
        print("Invalid day")

Output:

command
C:\Users\Your Name> python match_case.py
It's a weekday
command
C:\Users\Your Name> python match_case.py
It's a weekday

In the above example, we have used the match case statement to compare the value of the variable dayday against a list of patterns. Since the value of dayday is MondayMonday, the block of code under the case "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday":case "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday": is executed.

Match Case Statement with Conditional Statement

The match case statement can be combined with the if-else statement. The following example demonstrates how to combine the match case statement with the if-else statement:

match_case.py
# match case statement
x = 10
match x:
    case x < 10:
        print("x is less than 10")
    case x > 10:
        print("x is greater than 10")
    case x == 10:
        print("x is equal to 10")
    case _:
        print("Invalid value")
match_case.py
# match case statement
x = 10
match x:
    case x < 10:
        print("x is less than 10")
    case x > 10:
        print("x is greater than 10")
    case x == 10:
        print("x is equal to 10")
    case _:
        print("Invalid value")

Output:

command
C:\Users\Your Name> python match_case.py
x is equal to 10
command
C:\Users\Your Name> python match_case.py
x is equal to 10

In the above example, we have used the match case statement to compare the value of the variable xx against a list of patterns. Since the value of xx is 1010, the block of code under the case x == 10:case x == 10: is executed.

Match Case Statement with if-elif-else Statement

The match case statement can be combined with the if-elif-else statement. The following example demonstrates how to combine the match case statement with the if-elif-else statement:

match_case.py
# match case statement
day = "Monday"
x = 10
match day:
    case "Saturday" if x == 10:
        print("It's a weekend")
    case "Sunday" if x == 10:
        print("It's a weekend")
    case "Monday" if x > 10:
        print("It's a weekday")
    case "Monday" if x < 10:
        print("It's a not a weekday")
    case "Monday" if x == 10:
        print("It's a fun")
    case _:
        print("Invalid day")
match_case.py
# match case statement
day = "Monday"
x = 10
match day:
    case "Saturday" if x == 10:
        print("It's a weekend")
    case "Sunday" if x == 10:
        print("It's a weekend")
    case "Monday" if x > 10:
        print("It's a weekday")
    case "Monday" if x < 10:
        print("It's a not a weekday")
    case "Monday" if x == 10:
        print("It's a fun")
    case _:
        print("Invalid day")

Output:

command
C:\Users\Your Name> python match_case.py
It's a fun
command
C:\Users\Your Name> python match_case.py
It's a fun

In the above example, we have used the match case statement to compare the value of the variable dayday against a list of patterns. Since the value of dayday is MondayMonday and the value of xx is 1010, the block of code under the case "Monday" if x == 10:case "Monday" if x == 10: is executed.

Nested Match Case Statement

The match case statement can be nested inside another match case statement. The following example demonstrates how to nest the match case statement:

match_case.py
# match case statement
day = "Monday"
x = 10
match day:
    case "Saturday" | "Sunday":
        match x:
            case 10:
                print("It's a weekend")
            case _:
                print("It's a not a weekend")
    case "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday":
        match x:
            case 10:
                print("It's a weekday")
            case _:
                print("It's a not a weekday")
    case _:
        print("Invalid day")
match_case.py
# match case statement
day = "Monday"
x = 10
match day:
    case "Saturday" | "Sunday":
        match x:
            case 10:
                print("It's a weekend")
            case _:
                print("It's a not a weekend")
    case "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday":
        match x:
            case 10:
                print("It's a weekday")
            case _:
                print("It's a not a weekday")
    case _:
        print("Invalid day")

Output:

command
C:\Users\Your Name> python match_case.py
It's a weekday
command
C:\Users\Your Name> python match_case.py
It's a weekday

In the above example, we have used the match case statement to compare the value of the variable dayday against a list of patterns. Since the value of dayday is MondayMonday, the block of code under the case "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday":case "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday": is executed. The match case statement is nested inside the block of code under the case "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday":case "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday":. Since the value of xx is 1010, the block of code under the case 10:case 10: is executed.

Match Case Statement with list

The match case statement can be used with a list. The following example demonstrates how to use the match case statement with a list:

match_case.py
# match case statement
match [1, 2, 3]:
    case [1, 2, 3]:
        print("List is [1, 2, 3]")
    case [4, 5, 6]:
        print("List is [4, 5, 6]")
    case [7, 8, 9]:
        print("List is [7, 8, 9]")
    case _:
        print("Invalid list")
match_case.py
# match case statement
match [1, 2, 3]:
    case [1, 2, 3]:
        print("List is [1, 2, 3]")
    case [4, 5, 6]:
        print("List is [4, 5, 6]")
    case [7, 8, 9]:
        print("List is [7, 8, 9]")
    case _:
        print("Invalid list")

Output:

command
C:\Users\Your Name> python match_case.py
List is [1, 2, 3]
command
C:\Users\Your Name> python match_case.py
List is [1, 2, 3]

In the above example, we have used the match case statement to compare the value of the list [1, 2, 3][1, 2, 3] against a list of patterns. Since the value of the list [1, 2, 3][1, 2, 3] is [1, 2, 3][1, 2, 3], the block of code under the case [1, 2, 3]:case [1, 2, 3]: is executed.

Conclusion

In this guide, we explored the syntax and usage of match case statement in Python. Now that you have a solid understanding of match case statement in Python, you can use it to write flexible and dynamic Python programs. As you delve deeper into Python programming, experiment with different match case statements, explore their applications in real-world scenarios, and use them to enhance the efficiency and clarity of your code. For more hands-on examples and in-depth tutorials, explore our resources on Python Central Hub!

Was this page helpful?

Let us know how we did