match — Easy Playground
Begins a structural pattern matching block (3.10+)
Python Playground
# 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}")
Output
Click "Run" to execute your code
match/case is Python's structural pattern matching (3.10+). The _ wildcard matches anything. Use | for OR patterns.
Challenge
Try modifying the code above to explore different behaviors. Can you extend the example to handle a new use case?