Skip to main content

Match-case

Introduced in Python 3.10, match-case is a powerful alternative to long if/elif/else chains. It evaluates an expression and compares it against multiple case patterns, checking them in order from top to bottom.

Basic Pattern Matching

Use match-case for menu-driven logic where a variable can have several distinct literal values. It is much more readable than if/elif when branching based on a single expression.

seat_type = "ac"

match seat_type:
case "sleeper":
print("Sleeper: No AC, but has beds.")
case "ac":
print("AC: Climate controlled.")
case "general":
print("General: Budget seating.")
case _:
print("Unknown seat type.")
AC: Climate controlled.

The Wildcard Pattern

The underscore _ is a special pattern that matches anything. It acts as the mandatory "wildcard" or fallback if nothing else matches, preventing our logic from falling through silently.