Python Switch Case Statements

How to use Switch Case Statements Python

Unlike many programming languages, you may know like Java and C, Python does not support switch case construct. I came to know about this while converting a Java program to Python program from a git repository.

In this post, I am going to share how to implement switch case problems in Python so that you can solve the problems like in other programming languages.

This is the result of what I have learned from reading several good blog posts.

I checked in the official documentation of Python for why there is no switch case construct. According to it, in Python, you can easily achieve what switch constructs perform in other languages using a sequence of if... elif... elif... else.

Let us look into how a Python function looks like while using if... elif... elif... else statements:

For instance, you have a car showroom and you are creating a function to get different types of cars with different colors. Initially, you are planning to get Ford, Audi, Volkswagen, Mercedes or any car in pink color.

def get_car_color(car):
    if car == 'Ford':
        return ['Red', 'Black', 'White', 'Silver', 'Gold']
    elif car == 'Audi':
        return ['Black', 'White']
    elif car == 'Volkswagen':
        return ['Purple', 'Orange', 'Green']
    elif car == 'Mercedes':
        return ['Black']
    else:
        return ['Pink']

Later you would think of other brands and elifs in the function would increase with the increase of the brands you think of.

So, the program would lose its elegance. Python Lovers would say losing elegant (short, compact and readable) characteristics of Python is as good as coding in other languages.

 

Better Alternative

if... elif... elif... else can be replaced by using the concept of dictionaries.

car_name_and_colors = {
  'Ford': ['Red', 'Black', 'White', 'Silver', 'Gold'], 
  'Audi': ['Black', 'White'], 
  'Volkswagon': ['Purple', 'Orange', 'Green'], 
  'Mercedes': ['Black'],
}

def get_car_color(car):
    return car_name_and_colors[car]

What is wrong in the above code?

It does not take the case of the default case. In switch case statements, the default case contains the block of codes that must be executed when none of the cases are being matched.

car_name_and_colors = {
  'Ford': ['Red', 'Black', 'White', 'Silver', 'Gold'], 
  'Audi': ['Black', 'White'], 
  'Volkswagon': ['Purple', 'Orange', 'Green'], 
  'Mercedes': ['Black'],
}

def get_car_color(car):
    return car_name_and_colors[car, 'Pink']

In the above code, Pink would get returned if nothing gets matched in the dictionary car_name_and_colors.

Now the program would look more compact and readable.

Conclusion

According to a blog post, “Switch case is very useful programming construct that not only provides better performance than an if-else statement but also leaves you with a more manageable code”. I hope this post would help you in solving problems that require switch cases while programming in Python.