Technical skill practice

Python Switch Alternatives: match, if and a Lookup Dictionary

Python does not use a switch statement spelled switch. For simple value-to-value dispatch, a dictionary can be enough. For branching, use if/elif/else or, on Python 3.10 and later, match/case. Structural matching supports more than a traditional switch, so start with clear literal cases.

Start with a small, explicit task

Route three fictional control commands to display instructions. Two commands share a response, one has its own response, and everything else receives a default. Compare literal match cases with a small lookup mapping. The script returns text only; it does not control equipment or execute a command.

Run the complete example

This original example was checked with Python 3.12.14. Save it as python_switch.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.

Expected output

['wait', 'continue', 'unknown']

def route(command):
    match command:
        case "pause" | "hold":
            return "wait"
        case "resume":
            return "continue"
        case _:
            return "unknown"
lookup = {"pause": "wait", "hold": "wait", "resume": "continue"}
commands = ["pause", "resume", "stop"]
results = [route(command) for command in commands]
assert results == ["wait", "continue", "unknown"]
assert results == [lookup.get(command, "unknown") for command in commands]
assert route("hold") == "wait"
print(results)

Explain the result

pause and hold are quoted string literals, and the vertical bar makes them alternatives for one response. The wildcard underscore handles stop because no earlier literal matches. There is no fall-through requiring a break after each return. The dictionary expresses the same fixed mapping with less branching in this particular task.

An if/elif chain is useful when decisions involve ordered conditions rather than a fixed lookup, such as comparing several thresholds. match is useful when the shape of the input matters, but that larger pattern-matching task is outside this literal command example. Choose the construct that makes the actual decision readable rather than mechanically translating another language’s syntax.

Avoid these mistakes

An unquoted bare name in a pattern can capture a value instead of comparing it with a variable, so do not remove the quotes from these literal commands. This script requires Python 3.10 or later and was checked on 3.12; use an if chain or dictionary when targeting an older interpreter.

Questions, answered

Does Python require break after each case?

No. This match executes the matching case without C-style fall-through; each branch here also returns.

When is a dictionary simpler?

For a fixed mapping from a hashable command to a result, like the lookup shown here.

Sources and examples

ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.

Your experience. A complete resume.

Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.

Free account required. Free-plan PDF exports include a watermark.