1. match–case is similar to switch–case in C/Java. Introduced in Python 3.10.
  2. Use it when you want to choose one action from many based on ONE value (e.g. day number → day name).
  3. Syntax: match value: then case 1: ... case 2: ... case _: ... (underscore = default).
  4. Works like if/elif: checks case 1, then 2, then 3... until one matches; then runs that block and exits.
  5. You can match integers, strings, and other types.

Let’s go! 🚀


Part 1: Same Logic with if/elif

📌 Example: day number 1 → Sunday, 2 → Monday, ..., 7 → Saturday. Any other number → "Holiday".

With if/elif (long and repetitive):

day = int(input("Enter day number: "))
if day == 1:
    print("Sunday")
elif day == 2:
    print("Monday")
elif day == 3:
    print("Tuesday")
...
elif day == 7:
    print("Saturday")
else:
    print("Holiday")

Lots of "elif" for the same kind of check (value equals 1, 2, 3, ...). match–case is a cleaner way.


Part 2: match–case Syntax

📌 Syntax:

match value:
    case 1:
        ...    # do this when value is 1
    case 2:
        ...    # do this when value is 2
    case 7:
        ...
    case _:    # underscore = default (like else)
        ...
  • match is followed by the value you're checking (e.g. day).
  • case 1:, case 2:, etc. are the possible values. Only the first matching case runs.
  • case _: is the default (matches anything not matched above). Like "else".

Part 3: Demo – Day Number to Name

Simulating day numbers 1, 5, 9 with match–case:

for day in [1, 5, 9]:
    match day:
        case 1:
            name = "Sunday"
        case 2:
            name = "Monday"
        case 3:
            name = "Tuesday"
        case 4:
            name = "Wednesday"
        case 5:
            name = "Thursday"
        case 6:
            name = "Friday"
        case 7:
            name = "Saturday"
        case _:
            name = "Holiday"
    print(f"   Day {day} → {name}")

Output:

Day 1 → Sunday

Day 5 → Thursday

Day 9 → Holiday


Part 4: How It Runs (Like if/elif)

📌 Python checks case 1, then case 2, then case 3, ... until one matches. Then it runs that block and leaves the match. So it behaves like if/elif/else, not like a "jump table" in C/Java.


Part 5: case _ (Default)

📌 case _: means "match anything else." So it's your default. Put it last. The underscore _ is a special pattern that matches any value. So numbers not 1–7, or invalid input, can all be handled by case _: print("Holiday").


Part 6: Other Types (String, etc.)

📌 You can match integers, strings, and more. Example with string:

match choice:
    case "yes":
        print("OK")
    case "no":
        print("Cancel")
    case _:
        print("Unknown")

Demo: match string 'a', 'b', 'x'

for choice in ["a", "b", "x"]:
    match choice:
        case "a":
            result = "Option A"
        case "b":
            result = "Option B"
        case _:
            result = "Other"
    print(f"   '{choice}' → {result}")

Part 7: Summary

match value: ... case 1: ... case 2: ... case _: ... (clean alternative to long if/elif chains).

case _ is the default (like else). Requires Python 3.10+.

✅ Works like if/elif: first matching case runs, then exit. Can match int, str, etc.


Congratulations! You now understand match–case (Python’s switch-case) in Python! 🎉

Key Takeaways:

  • match–case is cleaner for many equal checks on one value
  • case _: is your default (always put it last)
  • First matching case runs and exits — just like if/elif
  • Works with numbers, strings, and more

Next Steps:

  • Try matching strings (menu choices)
  • Combine with previous lessons (day name + prime check)
  • Explore advanced patterns (lists, tuples, guards) in future guides