match value: followed by case pattern: blocks compares the value against each pattern in order and runs the first that fits. case "saturday" | "sunday": matches either string; case _: is the catch-all. It is Python 3.10 and newer, and it reads better than a long elif chain when you are testing one value against fixed options.
Patterns can also capture and destructure (case [x, y]: or case {"type": t}:), but the fixed-values form is the one you will use most. Normalize the input first, for example with .lower(), so "Saturday" and "saturday" match the same case.
def http_text(code):
match code:
case 200:
return "OK"
case 404:
return "Not found"
case _:
return "Something else"Your task
Write day_type(day) that returns "weekend" for saturday or sunday, "weekday" for the other five days, and "unknown" for anything else. It should not care about capital letters.
Hint 1
case "saturday" | "sunday": return "weekend"
Hint 2
List all five weekdays in one case separated by |.
Show the solution
def day_type(day):
match day.lower():
case "saturday" | "sunday":
return "weekend"
case "monday" | "tuesday" | "wednesday" | "thursday" | "friday":
return "weekday"
case _:
return "unknown"