Python Match Case Statement

In the last lesson you learned Nested Conditions. There you put one if inside another to handle layered choices. That works fine. But when you keep checking the same value against many options, the code starts to feel heavy. There is a cleaner way to write that. It is called match-case, and that is what this lesson is about.

๐Ÿค” Why Match Case?

Say you are building a menu. Someone picks a number, and each number does something different. With if/elif you end up writing the same variable name again and again.

Here is what that looks like with a long if/elif chain.

choice = "3"
if choice == "1":
print("You picked Coffee")
elif choice == "2":
print("You picked Tea")
elif choice == "3":
print("You picked Juice")
elif choice == "4":
print("You picked Water")
else:
print("Not on the menu")

See how choice == repeats on every line? It works. But your eyes have to read past the same thing over and over. The more options you add, the noisier it gets.

The fix is match-case. You name the value once at the top. Then you list the options under it. It is cleaner to read, and easier to add to later.

๐Ÿงฉ What Is Match Case?

match-case lets you take one value and compare it against several possible options. Those options are called patterns. A pattern is just a value you are checking for. You write the value once, then list each option as its own case.

Think of it like a vending machine. You press one button. The machine looks at that single press and decides which item to drop. You name your choice once, and it matches it and acts.

Here is the basic shape of it.

match value:
case option1:
# do this when value equals option1
case option2:
# do this when value equals option2
case _:
# do this when nothing above matched

A few things to notice here:

  • match value: names the thing you are checking, once.
  • Each case lists one option to compare against.
  • When a case matches, Python runs the code under it and then stops. It does not fall through to the others.
  • The case _: at the bottom is the default. The single underscore _ means โ€œanything elseโ€. It runs when none of the options above matched, like the else in an if chain.

Caution

match-case was added in Python 3.10. On an older version it will not run, and you will see a SyntaxError. Check your version with python --version. If it says 3.10 or higher, you are good.

๐Ÿน A Real Example

Letโ€™s rewrite that menu from the top, the clean way.

choice = "3"
match choice:
case "1":
print("You picked Coffee")
case "2":
print("You picked Tea")
case "3":
print("You picked Juice")
case "4":
print("You picked Water")
case _:
print("Not on the menu")

When you run this, Python looks at choice, which is "3". It walks down the cases. It skips "1" and "2", lands on "3", runs that line, and stops.

Output

You picked Juice

Notice how choice only appears once now, right at the match line. Every option below it is just the value you care about. That is the whole win here. Less repeating, and easier to scan.

๐Ÿ“… Another Example: Day Of The Week

Here is one more, so the pattern sticks. We take a short day code and turn it into the full name.

day = "wed"
match day:
case "mon":
print("Monday")
case "tue":
print("Tuesday")
case "wed":
print("Wednesday")
case "thu":
print("Thursday")
case "fri":
print("Friday")
case _:
print("That is the weekend")

The value day is "wed". So Python matches the third case and prints the full name.

Output

Wednesday

If day had been "sat" or anything not listed, the case _: would catch it and print the weekend line instead. That default keeps your program from silently doing nothing when an unexpected value shows up.

๐Ÿค Matching Several Values At Once

Sometimes a few different inputs should do the same thing. You do not need a separate case for each one. You can join them with a single |, which here means โ€œorโ€.

This example treats both "y" and "yes" as a yes.

answer = "yes"
match answer:
case "y" | "yes":
print("Great, moving on")
case "n" | "no":
print("Okay, stopping here")
case _:
print("Please answer yes or no")

The first case says this. If answer is "y" or "yes", run this line. Since answer is "yes", it matches and prints the first line.

Output

Great, moving on

This keeps related options together. You no longer spread them across many near-identical cases.

โš–๏ธ Match Case vs If/Elif

Both tools do similar work. So when should you reach for which? Here is a simple way to decide.

Situation Better choice
Checking one value against many fixed options match-case
Checking ranges, like a score greater than 90 if/elif
Each branch checks a different variable or condition if/elif
You are on Python older than 3.10 if/elif

So match-case shines when one value gets compared to many set options. For ranges and mixed conditions, plain if/elif is still the right tool.

โš ๏ธ Common Mistakes

A few things trip people up the first time. Watch for these.

Forgetting the colon. Both match and each case need a : at the end. Miss one and you get a SyntaxError.

# โŒ Avoid: missing colons
match day
case "mon"
print("Monday")
# โœ… Good: colon after match and after each case
match day:
case "mon":
print("Monday")

Wrong indentation. The code under each case must be indented, just like the body of an if. If you line it up with the case keyword, it breaks.

Using case else instead of case _. The default is a single underscore, not the word else.

# โŒ Avoid: there is no "case else"
case else:
print("Anything else")
# โœ… Good: the default is an underscore
case _:
print("Anything else")

Running on old Python. If you see a SyntaxError pointing at the match line and your code looks correct, check your version. match-case needs 3.10 or newer.

โœ… Best Practices

Keep these habits and your match-case blocks stay clean.

  • Almost always add a case _: at the end. It handles the values you did not plan for, so your program never just goes quiet.
  • Reach for match-case when you are comparing one value to a set list of options. If you find yourself checking ranges or different variables per branch, go back to if/elif.
  • Group related options with | instead of repeating near-identical cases.
  • Keep the code under each case short. If a case grows large, move that work into a function and call it from the case.

๐Ÿงฉ What Youโ€™ve Learned

โœ… match-case compares one value against many patterns, a cleaner alternative to a long if/elif chain.

โœ… You write the value once after match, then list each option as its own case.

โœ… The single underscore case _: is the default that catches anything not matched above.

โœ… You can group options that should act the same with |, which means โ€œorโ€.

โœ… match-case needs Python 3.10 or newer, and it suits fixed options better than ranges.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    What does the single underscore in `case _:` mean?

    Why: `case _:` is the default branch, like `else` in an if chain. It runs when no earlier case matched.

  2. 2

    Which Python version first added the match-case statement?

    Why: match-case was introduced in Python 3.10, so it will not run on older versions.

  3. 3

    How do you make one case match both "y" and "yes"?

    Why: The `|` symbol joins options in a single case and means "or", so either value matches.

  4. 4

    When is plain if/elif a better fit than match-case?

    Why: match-case compares one value to set options; for ranges and mixed conditions, if/elif is the right tool.

๐Ÿš€ Whatโ€™s Next?

You now have a clean way to handle many options for one value. Next up is a neat shortcut for writing a simple choice in a single line.

Ternary Operator

Share & Connect