Skip to content

Python Match Case Statement

  • by

The “match case” statement is a feature introduced in Python 3.10 (released in October 2021) to provide pattern-matching capabilities. It allows you to perform structural pattern matching, where you can compare the structure of a value against different patterns and execute corresponding code based on the match.

Pattern matching is a powerful programming concept that simplifies code by allowing you to handle different cases or structures of data in a more concise and readable way.

Here’s the basic syntax of the match case statement:

match value:
    case pattern_1:
        # Code to execute if pattern_1 matches
    case pattern_2 if condition:
        # Code to execute if pattern_2 matches and condition is True
    case pattern_3 as variable:
        # Code to execute if pattern_3 matches and bind to 'variable'
    case _:
        # Code to execute if no patterns match

Note: the “match case” statement was introduced in Python 3.10, so you’ll need to use Python 3.10 or a later version to use this feature.

Python Match Case Statement Example

Here’s an example usage of the match case statement:

def classify_age(age):
    match age:
        case 0:
            return "Newborn"
        case n if 1 <= n <= 12:
            return "Child"
        case n if 13 <= n <= 19:
            return "Teenager"
        case n if n >= 20:
            return "Adult"
        case _:
            return "Invalid age"

print(classify_age(5))    # Output: "Child"
print(classify_age(15))   # Output: "Teenager"
print(classify_age(30))   # Output: "Adult"
print(classify_age(-5))   # Output: "Invalid age"

Output:

Python Match Case Statement

Let’s imagine a simple program that processes different shapes and calculates their areas based on the given data.

from math import pi

def calculate_area(shape):
    match shape:
        case "circle", radius:
            return pi * radius ** 2
        case "rectangle", width, height:
            return width * height
        case "triangle", base, height:
            return 0.5 * base * height
        case _:
            return "Invalid shape"

# Calculate areas
print(calculate_area(("circle", 5)))                 # Output: 78.53981633974483
print(calculate_area(("rectangle", 3, 4)))           # Output: 12
print(calculate_area(("triangle", 6, 8)))            # Output: 24.0
print(calculate_area(("square", 5)))                 # Output: Invalid shape

Arithmetic operation

def evaluate_expression(expr):
    match expr:
        case "add", a, b:
            result = a + b
        case "subtract", a, b:
            result = a - b
        case "multiply", a, b:
            result = a * b
        case "divide", a, b:
            result = a / b
        case _:
            result = "Invalid operation"
    return result

print(evaluate_expression(("add", 5, 3)))  # Output: 8
print(evaluate_expression(("subtract", 10, 4)))  # Output: 6
print(evaluate_expression(("divide", 20, 5)))  # Output: 4.0
print(evaluate_expression(("modulus", 7, 2)))  # Output: Invalid operation

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading