Python Match

Python 3.10 introduced the powerful match statement — Python’s take on structural pattern matching. Similar to switch-case statements in other languages, match helps you write cleaner, more readable, and more efficient conditional logic.

In this guide, we’ll explore:

  • What is the match statement?
  • Syntax and usage
  • Real-world examples
  • Best practices

What is the Python match Statement?

The match statement allows you to compare a given value (called a subject) against different patterns and execute code based on the match.

It’s similar to switch-case in languages like Java, C++, and JavaScript — but Python’s version is more powerful thanks to pattern matching.

Python match Statement Syntax

match subject:
    case pattern1:
        # code block
    case pattern2:
        # code block
    case _:
        # default case
  • match: keyword to start the pattern match
  • subject: the variable or expression to match against
  • case: each pattern to test
  • _: wildcard (default/fallback) case

Python match Statement Example

def http_status(status):
    match status:
        case 200:
            return "OK"
        case 404:
            return "Not Found"
        case 500:
            return "Server Error"
        case _:
            return "Unknown Status"

print(http_status(404))  # Output: Not Found

Matching Multiple Patterns

def check_day(day):
    match day:
        case "Saturday" | "Sunday":
            return "Weekend"
        case "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday":
            return "Weekday"
        case _:
            return "Invalid day"

Matching Data Structures (Tuples, Lists, Dicts)

Tuple Matching

point = (0, 1)

match point:
    case (0, 0):
        print("Origin")
    case (0, y):
        print(f"Y={y}")
    case (x, 0):
        print(f"X={x}")
    case (x, y):
        print(f"Point at X={x}, Y={y}")

Matching with Conditions (if Guards)

def check_number(x):
    match x:
        case x if x < 0:
            return "Negative"
        case x if x == 0:
            return "Zero"
        case x if x > 0:
            return "Positive"

Python match Statement Requirements

  • Available from Python 3.10+
  • Cannot be used in earlier versions
  • Uses pattern matching, not simple comparisons

To check your version:

python --version

Why Use match Instead of if-elif?

match Benefitsif-elif Limitations
Cleaner for complex logicBecomes hard to manage
Better structureMore verbose for simple checks
Pattern-matching supportNo structural pattern support

Best Practices for Using match

  • Use _ as a default fallback case
  • Keep case blocks simple and readable
  • Use pattern matching for tuples, enums, or classes
  • Always ensure you’re on Python 3.10 or higher

Real-World Use Cases

  • API status code responses
  • State machine implementations
  • Handling JSON or dictionary-like data
  • Simplifying large conditional blocks

Try It Yourself

Want to try match statement examples online? Visit The Coding College for hands-on Python code editors and tutorials tailored for every level of learner.

More Python Topics

Explore more beginner-to-advanced Python topics on:

Conclusion

The Python match statement is a robust, flexible feature that enhances code readability and clarity. If you’re writing complex conditional logic, match is a modern and Pythonic solution that’s worth mastering.

Leave a Comment