match

Soft KeywordPython 3.10+Advanced

Begins a structural pattern matching block (3.10+)

Quick Info

Documentation
Official Docs
Python Version
3.10+
Install
N/A — requires Python 3.10+

Learn by Difficulty

Quick Example

python
# Simple value matching
status = 404
match status:
    case 200:
        print("OK")
    case 301:
        print("Redirect")
    case 404:
        print("Not Found")
    case 500:
        print("Server Error")
    case _:
        print(f"Other: {status}")

# Matching with variables
command = "quit"
match command:
    case "start":
        print("Starting...")
    case "stop":
        print("Stopping...")
    case "quit" | "exit":
        print("Goodbye!")
    case _:
        print(f"Unknown: {command}")

match/case is Python's structural pattern matching (3.10+). The _ wildcard matches anything. Use | for OR patterns.

Try in Playground

Tags

languagesyntaxcorecontrol-flowpattern-matching

Related Items