The match statement compares a value against several patterns, similar to switch in other languages.
case "value": matches a literal.
case 1 | 2: matches either value.
case _: is the default when nothing else matches.
Patterns can also unpack tuples, lists and dictionaries.
💡 Key Takeaways: Python Match
Mastering Python Match helps you write clean and reliable Python code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
Python Match Code Example
Python
def http_status(code):
match code:
case 200:
return "OK"
case 301 | 302:
return "Redirect"
case 404:
return "Not Found"
case 500:
return "Server Error"
case _:
return "Unknown"
for code in [200, 302, 404, 418]:
print(code, "→", http_status(code))
point = (0, 5)
match point:
case (0, y):
print("On the Y axis at", y)
case (x, 0):
print("On the X axis at", x)
case _:
print("Somewhere else")
Free sign-in required · Google login available
Python Match Practice Exercises
Check what you learned in “Python Match”.
/ 3 solved
Fill in the blank
Write the default case.
match code:
case 200:
print("OK")
case :
print("Other")
✓ Correct!Not quite. Try again.
Multiple choice
Which Python version introduced match?
✓ Correct!Not quite. Try again.
Code challenge
Use match on day: print "Weekend" for "Sat" or "Sun", otherwise "Weekday".
Write the code in the live editor. Your answer is checked automatically.