Stop 03 · Control FlowTopics · 4/8

The match Statement

Part of the Python Roadmap

In short

When one value can be one of many options, like a menu choice or a command, a long if/elif chain gets noisy. Python 3.10 added match, which reads like a clean menu and can even look inside tuples and dicts.

match (Python 3.10+) compares a value against patterns in case blocks, like a powerful switch. It can match literals, several options with |, and even the shape of tuples, lists and dicts.

The jaadu sorts envelopes into a wall of wooden pigeonholes, mid-toss with a letter flying towards the right slot.

Think of it like this

Sorting letters at the post office

A postal worker looks at each letter and drops it into the right pigeonhole: Mumbai, Delhi, Chennai, or "Other" for everything else. Some pigeonholes take two cities, some only take parcels of a certain shape. match is that sorting rack: one value, many labelled slots, and a catch-all at the end.

Watch it happen

match command, with command = "go"

"start" | "go"
0
"stop"
1
"help"
2
_
3

1/4The value "go" is compared with each case pattern, top to bottom.

The big ideas

What you need to know

case with literals

case "stop": matches that exact value. Numbers, strings, True, False and None all work as literal patterns.

| means or

case "start" | "go": matches either value, so you don't repeat the same block twice.

Match the shape

case (0, y): matches any two-item tuple whose first item is 0, and captures the second item into y. It works for lists and dicts too.

_ is the catch-all

case _: matches anything, like else. Always add one so unexpected values are handled.

See it in code

Walk through the code

python
1command = input("Command: ").strip().lower()2 3match command:4    case "start" | "go":5        print("Starting the ride")6    case "stop":7        print("Stopping")8    case "help":9        print("Commands: start, stop, help")10    case _:11        print(f"Unknown command: {command}")12 13point = (0, 5)14 15match point:16    case (0, 0):17        print("At the origin")18    case (0, y):19        print(f"On the y-axis at {y}")20    case (x, y):21        print(f"At x={x}, y={y}")

1/5Clean the input first so " STOP " still matches "stop".

Requires Python 3.10 or newer. The second match prints "On the y-axis at 5".

Save yourself the pain

Common mistakes

Oops

Thinking case some_variable: compares against that variable's value.

Do this instead

A bare name in a pattern captures the value instead of comparing. Use literals, or dotted names like Colour.RED, for comparison.

Oops

Forgetting case _: and silently ignoring unexpected input.

Do this instead

Always end with case _: and print a helpful message.

Oops

Using match on Python 3.9 or older and getting a SyntaxError.

Do this instead

match needs Python 3.10+. Check with python --version.

Your mission

A chai stall menu

~20 min

Quick check

What does case "tea" | "chai": do?

Go deeper

  1. 1Rewrite a long if/elif chain that checks a menu choice as a match statement.
  2. 2Use case _: as the default branch.
  3. 3Try matching a tuple like (x, 0) to see structural pattern matching.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge