Python
switch statement
case statement
programming
duplicate

What is the Python equivalent for a case/switch statement?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Python historically had no direct switch statement, so developers used if chains or dictionary-based dispatch. Modern Python also has match and case, which provide structural pattern matching and cover many situations where people previously wanted a switch-like construct.

The Simple Answer: Use if and elif

For a small number of branches, plain control flow is still the clearest option.

python
1def handle_status(code):
2    if code == 200:
3        return "ok"
4    elif code == 404:
5        return "not found"
6    elif code == 500:
7        return "server error"
8    else:
9        return "unknown"
10
11print(handle_status(404))

This is easy to read and perfectly idiomatic when the branch count is small.

Dispatch Dictionaries

If you are selecting behavior by exact keys, a dictionary of callables is often cleaner than a long if ladder.

python
1def start():
2    return "starting"
3
4def stop():
5    return "stopping"
6
7def unknown():
8    return "unknown command"
9
10handlers = {
11    "start": start,
12    "stop": stop,
13}
14
15command = "start"
16result = handlers.get(command, unknown)()
17print(result)

This works well when:

  • the cases are simple exact matches
  • each branch maps naturally to a function
  • you want easy extensibility

match and case in Modern Python

Python 3.10 introduced structural pattern matching, which is the closest built-in answer to a traditional switch statement.

python
1def describe_http(code):
2    match code:
3        case 200:
4            return "ok"
5        case 404:
6            return "not found"
7        case 500:
8            return "server error"
9        case _:
10            return "unknown"
11
12print(describe_http(500))

For exact constant matching, this reads very much like a switch statement. But it can also do far more than classic switch syntax.

Why match Is More Powerful Than Switch

Structural pattern matching can destructure tuples, lists, dictionaries, and custom objects.

python
1def route(event):
2    match event:
3        case {"type": "email", "to": recipient}:
4            return f"send email to {recipient}"
5        case {"type": "sms", "to": recipient}:
6            return f"send sms to {recipient}"
7        case _:
8            return "unknown event"
9
10print(route({"type": "email", "to": "[email protected]"}))

This is not just a switch by value; it is pattern matching over data shape. That is why it is often better described as a new control-flow feature rather than a direct clone of switch.

Which Approach Should You Choose

A reasonable guideline is:

  • use if and elif for a few simple branches
  • use a dispatch dictionary for direct value-to-function routing
  • use match and case when pattern matching or clearer branch syntax helps

There is no need to force one style everywhere. The best option depends on the shape of the problem.

Common Pitfalls

The biggest mistake is assuming Python always needs a switch replacement. Often a short if chain is already the clearest code.

Another issue is overusing dispatch dictionaries when the branches need rich conditions rather than simple exact key lookup.

Developers also misunderstand match as "just switch." It is more powerful, but that power can make code harder to read if used for trivial cases where an if would do.

Finally, remember that match requires Python 3.10 or later. If you are targeting older runtimes, stick with if chains or dictionary dispatch.

Summary

  • Small branch sets are often best handled with if and elif.
  • Dictionary dispatch is a clean switch-like pattern for exact key lookup.
  • Python 3.10 introduced match and case for structural pattern matching.
  • 'match is more powerful than a classic switch because it can match data shapes, not just values.'
  • Choose the style that makes the branching logic clearest, not the one that most resembles another language.

Course illustration
Course illustration

All Rights Reserved.