Python Switch Case – Switch Statement Example


In languages like C, C++, and Java, the switch statement helps in comparing a value to different constants and running specific code for each case. Though Python doesn’t have a ‘switch,’ we can work around it using other techniques, let’s switch on.

Use of Switch Statement

So, in programming languages like C, C++, and Java, there’s this cool feature called the “switch” statement. It’s like a tool that lets you take a value (like a number or a variable) and compare it to a bunch of different possible values.

Depending on which value it matches with, you can make the program do different things. It’s handy when you have multiple cases to handle and you want to avoid writing a bunch of if-else statements.


Now, when it comes to Python, it doesn’t have this exact “switch” tool. But don’t worry, Python developers are clever! They’ve come up with different tricks to get similar results. Instead of using a “switch,” they use other methods.

One common way is by using something called a dictionary. It’s like a fancy list that connects values with actions or code. So, if you have a certain value, you can look it up in the dictionary and run the associated code.

Another method is using a bunch of if-else statements, where you compare the value to different cases, just like how you’d use “switch” in other languages. It’s a bit more manual, but it gets the job done.

Python has also introduced a newer way called the “match” statement, which helps you do similar things as a “switch” statement. It’s not exactly the same, but it’s pretty powerful and makes handling different cases easier.

Also Read:  Multiprocessing for loop in Python

Dictionary-Based Approach

Imagine you have a bunch of keys, and each key opens a different door. In Python, you can use a dictionary to map keys (values) to doors (code). This is similar to how a switch works.

def open_door_a():
    return "You entered Door A."

def open_door_b():
    return "You opened Door B."

def default_door():
    return "That's not a valid door."

door_map = {
    1: open_door_a,
    2: open_door_b,

choice = 1
result = door_map.get(choice, default_door)()
You entered Door A.

If-Else Logic

Think of this like making decisions step by step. You have different choices, and you check each one to see which fits. This is one of the alternatives of switch statement in Python.

choice = 2

if choice == 1:
    result = "You chose Option 1."
elif choice == 2:
    result = "You picked Option 2."
    result = "That's not a valid choice."

You picked Option 2.

Python 3.10’s match Statement

Python 3.10 brought a new trick called match. It’s like magic patterns that help you decide what to do. It allows you to pattern match on the value of choice and execute different code blocks based on the matched patterns. Below code is to show you how it works.

choice = 'Hello, World!'

match choice:
    case 'Hello, World!':
        print('Hello to you too!')
    case 'Goodbye, World!':
        print('See you later')
    case other:
        print('No match found')
Hello to you too!

Keep in mind that the match statement is a new feature introduced in Python 3.10, and you need to make sure you are using at least Python 3.10 to run this code. It will not work even in Python 3.9.

Using Libraries

Sometimes, you can bring in helpers to make things easier. Just like having friends help you decide where to eat. Libraries like enum and switch can make your Python code feel more like a traditional switch statement. Let’s see one example of each library.

Also Read:  Python | List all files in a Directory

Using switch Library

# Install the library first (if not already installed)
# pip install switch

from switch import Switch

choice = 1

# Using the switch library
result = None

with Switch(choice) as case:
    if case(0):
        result = "You chose Option A."
    if case(1):
        result = "You went with Option B."
    if case.default:
        result = "That's not a valid option."

You went with Option B.

Using Enum Library

from enum import Enum, auto

class Choices(Enum):
    OptionA = auto()
    OptionB = auto()

choice = Choices.OptionB

result = {
    Choices.OptionA: "You chose Option A.",
    Choices.OptionB: "You went with Option B.",
}.get(choice, "That's not a valid option.")

You went with Option B.


Python does not have the exact functionality of switch statement like C++ or Java. In this article, we explored different alternative of switch case in Python like dictionary based approach, if else statement, match statement, etc.

This is it for this article. If you have something in your mind about this article, please let me know in the comment section below.

Leave a Comment

Your email address will not be published. Required fields are marked *