match — Easy Examples
Begins a structural pattern matching block (3.10+)
Pattern matching basics
Using match/case for structural pattern matching (Python 3.10+).
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}")
Expected Output
Not Found Goodbye!
match/case is Python's structural pattern matching (3.10+). The _ wildcard matches anything. Use | for OR patterns.
Want to try these examples interactively?
Open Easy Playground