PY-101 ยท Conditions
match statements
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
A match statement compares one value against a list of case patterns and runs the first one that fits. It is Python's version of a switch, added in Python 3.10. The case _ pattern catches anything that did not match.
- Write match and case
- Combine cases with |
- Use _ as the default
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
command = "go"match command:case "start" | "go":print("Starting...")case "stop":print("Stopping.")case _:print("Unknown command:", command)
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
Starting... ๐ฏ Use match on day to print "Weekend" for "sat" or "sun", "Workday" for anything else. day = "sun".
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
case "sat" | "sun": print("Weekend"), then case _: print("Workday")
- The problemA chatbot gets one-word commands: "help", "start", "stop", "quit". A long if/elif chain works, but match lists every command neatly in one place.
- The Python idea
match value:A match statement compares one value against a list of case patterns and runs the first one that fits. - In real lifeWeb servers route requests and games handle key presses with exactly this shape: one value, many possible actions.
Start with a question
A chatbot gets one-word commands: "help", "start", "stop", "quit". A long if/elif chain works, but match lists every command neatly in one place.
Command menus, status codes and message types are everywhere. match makes long lists of options easy to read.
Explain it like I'm 10
It is like a sorting machine for letters: it looks at each one and drops it into the box with the matching label. Anything without a label goes into the "other" box.
The technical explanation
match value: starts the block. Each case pattern: is checked from top to bottom, and only the first match runs.
case "start" | "go": matches either word. The | means "or" inside a pattern.
case _: is the wildcard. It always matches, so it works like else.
Syntax
match value:
case "a":
...
case "b" | "c":
...
case _:
... Real-world scenario
Web servers route requests and games handle key presses with exactly this shape: one value, many possible actions.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: for loops and range() โQuestions people ask
Is match the same as switch in Python?
It does what switch does and more: it can also match the shape of lists, tuples, dictionaries and objects. For plain values it reads like a switch.
Should I use match or if/elif?
Use match when you compare one value against many fixed options. Use if/elif for ranges and comparisons like score >= 90.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner Is match the same as switch in Python?
It does what switch does and more: it can also match the shape of lists, tuples, dictionaries and objects. For plain values it reads like a switch.
beginner Should I use match or if/elif?
Use match when you compare one value against many fixed options. Use if/elif for ranges and comparisons like score >= 90.
intermediate Which Python version added match?
3.10. Structural pattern matching arrived in Python 3.10.
intermediate What does case _ do?
Matches anything. _ is the wildcard pattern.
Quick revision
Remember
- Write match and case
- Combine cases with |
- Use _ as the default
case _:must come last. Anything below it can never run, because_matches everything.
Syntax
match value:
case "a":
...
case "b" | "c":
...
case _:
... Official documentation
- match statements (docs.python.org)