2 Simple Ways to Implement Python Switch Case Statement

1. Objective

In our last Python tutorial, we studied XML Processing in Python 3. Today, we will study How to implement Python Switch Case Statement. Unlike other languages like Java Programming Langauge and C++, Python does not have a switch-case construct. Along with this, we will see how to work a loophole for Python switch case statement.
So, let’s discuss different ways of Implementation for Python Switch Case Statement.

2 Simple Ways to Implement Python Switch Case Statement

2 Simple Ways to Implement Python Switch Case Statement

2. Python Switch Case Statement

Python does not have a simple switch case construct. Coming from a Java or C++ background, you may find this to be a bit odd.
In C++ or Java, we have something like this:

string week(int i){
               case 0:
                       return “Sunday”
               case 1:
                       return “Monday”
               case 2:
                       return “Tuesday”
               case 3:
                       return “Wednesday”
               case 4:
                       return “Thursday”
               case 5:
                       return “Friday”
               case 6:
                       return “Saturday”
                       return “Invalid day of week”

But Python does not have this. So, to get around this, we use Python’s built-in dictionary construct to implement cases and decided what to do when a case is met. We can also specify what to do when none is met.
Follow this link to know more about Python Dictionary
A switch case has been requested for Python, but never made it. You can refer to the following link to find out what happened:

Python Interview Questions

3. Solutions for Python Switch Case Statement

One way out would be to implement an if-elif-else ladder. Rather, we can use a dictionary to map cases to their functionality. Here, we define a function week() to tell us which day a certain day of the week is. A switcher is a dictionary that performs this mapping.

>>> def week(i):
         return switcher.get(i,"Invalid day of week")

Now, we make calls to week() with different values.

>>> week(2)


>>> week(0)


>>> week(7)

‘Invalid day of week’

>>> week(4.5)

‘Invalid day of week’

As you can see, for values other than the ones we mention in the switcher, it prints out “Invalid day of week”. This is because we tell it to do so using the get() method of a dictionary.
Do you know How can Python Send Email Via SMTP Server

a. Using Python Functions & Lambdas

We can also use functions and lambdas in the dictionary.

>>> def zero():
        return 'zero'
>>> def one():
        return 'one'
>>> def indirect(i):
        func=switcher.get(i,lambda :'Invalid')
        return func()
>>> indirect(4)


>>> indirect(2)


>>> indirect(1)


>>> indirect(0.5)


b. With Python Classes

Using this concept with classes lets us choose a method at runtime.

>>> class Switcher(object):
          def indirect(self,i):
                   method=getattr(self,method_name,lambda :'Invalid')
                   return method()
          def number_0(self):
                   return 'zero'
          def number_1(self):
                   return 'one'
          def number_2(self):
                   return 'two'
>>> s=Switcher()
>>> s.indirect(2)


>>> s.indirect(4)


>>> s.number_1()


So, this was all about Python Switch Case Statement. Hope you like our tutorial.

4. Conclusion

Hence, we conclude that Python does not have an in-built switch-case construct, we can use a dictionary instead. Furthermore, if you have any queries regarding Python Switch case Statements, feel free to ask in the comment section.
Related Topic – Python GUI Programming 
For reference

3 Responses

  1. Jayeswaran says:

    Will be good if we know the module name to use appropriate for the beginners.

    • Data Flair says:

      We Respect Your Opinion!
      But in this “Python Switch Case Statement” tutorial, we do not make use of any module to implement a switch case in Python. The two ways we choose are
      1. Python Functions
      2. Python classes.
      You do not need to import any module for this.

  2. Lagu Stephen says:

    You Rock!

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.