match/case — pattern matching (Python 3.10+).
1# Simple match2def http_status(status):3 match status:4 case 200:5 return "OK"6 case 404:7 return "Not Found"8 case 500:9 return "Server Error"10 case _:11 return "Unknown"1213# With variables14match command:15 case "quit":16 exit()17 case ["move", x, y]: # List18 move_to(x, y)19 case {"action": action, "target": target}: # Dict20 execute(action, target)21 case str() if len(command) > 10: # Guard22 process_long(command)23 case _: # Wildcard24 print("Unknown command")2526# OR pattern27match status:28 case 400 | 401 | 403:29 return "Client Error"30 case 500 | 502 | 503:31 return "Server Error"
Patterns: