Lesson 13 of 60
โฎPrev Nextโฏ

PY-101 ยท Conditions

match statements

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

13of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก 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.

    match statements, running
    1. command = "go"
    2. match command:
    3. case "start" | "go":
    4. print("Starting...")
    5. case "stop":
    6. print("Stopping.")
    7. case _:
    8. print("Unknown command:", command)
    Press โ–ถ to watch Python run this, one line at a time.
    1. 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.
    2. The Python ideamatch value:A match statement compares one value against a list of case patterns and runs the first one that fits.
    3. 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 _:
            ...

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    Web servers route requests and games handle key presses with exactly this shape: one value, many possible actions.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. Which Python version added match?
    2. 2. What does case _ do?
    3. 3. How do you match "y" or "yes" in one case?
    4. 4. How many cases run when several could match?
    5. 5. Where should case _ go?

    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