Structural Pattern Matching (match-case)
15 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Introduced in Python 3.10 (PEP 634), structural pattern matching (match-case) is a powerful control flow feature that goes far beyond traditional switch statements.
Basic match-case syntax
Match statement compares one value, called the subject, with each case pattern from top to bottom. It runs the block of the first case that matches. Python 3.10 added it.
A match statement inspects a subject value and executes the first matching case block.
status = 404match status: case 200: print("OK") case 404: print("Not Found") case _: print("Unknown Status")Example 1 · Order status messages
order_status = "shipped"
match order_status: case "placed": print("We received your order") case "shipped": print("Your order is on the way") case "delivered": print("Order delivered") case _: print("Unknown status")Your order is on the way
What this code does
- The subject is compared with each string in order. "shipped" matches the second case.
- Only that block runs. There is no break and no fall-through to the next case.
The underscore _ acts as a wildcard default case, matching anything that was not caught by previous cases.
Multiple patterns with the pipe | operator
Or pattern is several patterns joined with | on one case line. The case matches when the subject fits any one of them, so one block can serve many values.
Combine multiple matching values in a single case statement using the pipe | symbol.
command = "quit"match command: case "quit" | "exit" | "q": print("Terminating session") case "help" | "h": print("Displaying manual") case _: print("Command unrecognized")Example 2 · Grouping HTTP codes
http_code = 503
match http_code: case 200 | 201: print("Success response") case 400 | 401 | 403 | 404: print("Client error response") case 500 | 502 | 503: print("Server error response") case _: print("Unrecognized HTTP code")Server error response
What this code does
- Matches HTTP status code 503 against multiple error codes.
- Triggers the Server error response branch cleanly.
The pipe operator acts as an OR condition between matching patterns.
Pattern guards with if
Guard is an if condition written after a case pattern. The case matches only when the pattern fits and the guard is also True, which allows ranges and other tests.
You can attach an if guard to a case. The case succeeds only if both the pattern matches and the guard condition evaluates to true.
age = 22match age: case n if n < 18: print("Minor") case n if 18 <= n <= 65: print("Working age") case _: print("Senior")Example 3 · Fee bands with guards
weight_kg = 7
match weight_kg: case w if w <= 2: fee = 40 case w if w <= 5: fee = 80 case _: fee = 120print(f"Fee: {fee}")Fee: 120
What this code does
wtakes the value of the subject, and the guard tests it. 7 fails both guards.- The wildcard
_catches everything left, so fee is 120.
Guards provide powerful filtering without requiring nested if statements inside case bodies.
Next step
Check your understanding
8 quick questions