Site icon DataFlair

2 Simple Ways to Implement Python Switch Case Statement

Python course with 57 real-time projects - Learn Python

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 Language 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

What is 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){
       switch(i){
               case 0:
                       return “Sunday”
                       break;
               case 1:
                       return “Monday”
                       break;
               case 2:
                       return “Tuesday”
                       break;
               case 3:
                       return “Wednesday”
                       break;
               case 4:
                       return “Thursday”
                       break;
               case 5:
                       return “Friday”
                       break;
               case 6:
                       return “Saturday”
                       break;
               default:
                       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.

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):
        switcher={
                0:'Sunday',
                1:'Monday',
                2:'Tuesday',
                3:'Wednesday',
                4:'Thursday',
                5:'Friday',
                6:'Saturday'
             }
         return switcher.get(i,"Invalid day of week")

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

>>> week(2)

Output

‘Tuesday’
>>> week(0)

Output

‘Sunday’
>>> week(7)

Output

‘Invalid day of week’
>>> week(4.5)

Output

‘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.

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):
        switcher={
                0:zero,
                1:one,
                2:lambda:'two'
                }
        func=switcher.get(i,lambda :'Invalid')
        return func()
>>> indirect(4)

Output

‘Invalid’
>>> indirect(2)

Output

‘two’
>>> indirect(1)

Output

‘one’
>>> indirect(0.5)

Output

‘Invalid’

b. With Python Classes

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

>>> class Switcher(object):
          def indirect(self,i):
                   method_name='number_'+str(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)

Output

‘two’
>>> s.indirect(4)

Output

‘Invalid’
>>> s.number_1()

Output

‘one’

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

Python Interview Questions on Switch Case Statement

  1. Does Python have a switch case statement?
  2. How do you implement a switch case in Python?
  3. What is a case in switch statement in Python?
  4. Explain Python switch case statement with example.
  5. How many cases can you have in switch statement in Python?

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.

Exit mobile version