Python Switch Case Statement Tutorial – Three Ways To Implement Python Switch Case Statement

Dear python learners, in the last tutorial you have learned Convert Python To exe Tutorial . In this tutorial you will learn how to implement switch case statement in python. So welcome to Python Switch Case Statement Tutorial.

Switch case is a very powerful and important decision-making construct that are commonly used in modular programming. When you don’t want a conditional block cluttered with multiple if conditions, then, switch case can give a cleaner way to implement control flow in your program.

So let’s get ahead and see how can we implement switch case statement in python.

Python Switch Case Statement Tutorial – Getting Started

Unlike other programming languages like C, C++, java etc python does not have a switch case construct over the self. But don’t worry there are many  other constructs like dictionary, lambda function and classes are available to write switch case statement in python. PEP 3103   will explain you why python have not switch case statement.

A Typical Switch Case Statement in C++

Firstly, we will see how a switch case statement looks like in another languages. So take a look of C++ switch case statement.

But as we know Python does not have this. So, achieving 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.

Have you checked best python IDEs for Windows

Python Switch Case Statement Implementation – Dictionary Mapping, if-elif-else ladder, Classes

Now we will discuss about 3 simple ways of implementing python switch case statement. So let’s see it one-by-one.

Python Switch Case Statement Using Dictionary Mapping

First of all we will see how to implement python switch case statement using Dictionary mapping. So let’s see how to do that.

Now write the below code.

Explanation

So now we will discuss the above code step by step.

  • First of all you have to declare a switch method.
  • In switch( ) method what you have to perform is that you have to take radius and height as an input from the user and perform operation on it according to user’s selection.
  • So now declare first variable i.e., radius and second variable i.e., height.
  • Then show a message for choosing option.
  • Now take option input from the user.
  • Now define sub method for calculating surface area, literal area and volume of cylinder.
  • Then you have to create a dictionary that may be any name. And inside this, map the dictionary that means if user press option 1 then Sar( ) method will be performed, and if  user press option 2 then Lar( ) method will be performed  and so on.
  • Now you have to get option value so that  you can perform action according to it. So call the get() method on the dictionary object which returns the function matching the argument and invokes it simultaneously.
  • And at last call the switch( ) method.

So now let’s check the output –

Python Switch Case Statement
Python Switch Case Statement

So guys we have successfully construct python switch case statement using dictionary. Now move to another method.

Let’s check Best python books for beginners

Python Switch Case Statement Using if-elif-else ladder

Constructing python switch case statement using if-elif-else ladder is very much similar as the dictionary mapping. But here we don’t need to define sub methods and dictionary, instead we have to create if-elif-else construct. It works as a switch construct in python.

So let’s see it practically. Write the following code.

  • In this code above few lines are same as of the dictionary mapping so i am not explaining them here.
  • Here the most important thing is that you have to declare  if-elif-else ladder.
  • So if the option is 1 then you have to calculate surface area of cylinder, and if the option is 2 then you have to calculate literal surface area,  and if the option is 3 then you have to calculate volume, and if the option is not 1,2 or 3 then it will print incorrect option.

Now its time to check the output, so let’s see.

Python Switch Case Statement
Python Switch Case Statement

Python Calculator – Create A Simple GUI Calculator Using Tkinter

Python Switch Case Statement Using Class

So now we will see how to construct a switch case statement in python using class. So let’s start.

Write the following code –

Explanation

Now we will discuss what we have done in the above program.

  • At first we have created a class named PythonSwitchStatement  which defines switch( ) method. It also defines  some functions specific to different cases.
  • Switch( ) method takes the month as an argument, converts it to string and appends to the ‘case_’ literal. After that, the resultant string gets passed to the getattr() method.
  • The getattr( ) method returns a matching function available in the class.
  • And if the string doesn’t find a match, then the getattr() returns the lambda function as default.
Python Switch Case Statement
Python Switch Case Statement

So guys this is all about implementation of switch case statement in python.

Conclusion

Although python does not have an in-built switch-case construct, but we can construct it using dictionary mapping, class and if-elif-else ladder. So i am wrapping Python Switch Case Statement Tutorial here. I hope you have enjoyed this tutorial, so if you have any query regarding this then feel free to comment. Surely i will solve your problems, till then stay tuned with Simplified Python.

 

Leave a Comment