Switch Case in Python - Brain Mentors Skip to content

Switch Case in Python

Disclaimer : Before starting the blog let me tell you there is no switch case in Python.

So first let’s take a scenario that you want to build a menu driven program in python something like a calculator : 

  • Press 1 for addition
  • Press 2 for subtraction
  • Press 3 for division
  • Press 4 for multiplication

Now for each option in this menu there is a function defined in your program to perform the specific task.
Now what approach you will follow in python to build this kind of menu driven program.

Let me add one more thing here that if you are thinking that you will use If Else. Then it’s a No,
if else is not the solution here because in this example you have only 4 options in menu,
but what if you have 40 options then you can imagine how your program will look like. Nothing but a ladder of if else.

So what we are going to learn in this blog is that how to make a perfect replacement of switch case in python programming.

In python you can have a lot more better option then if else or switch case. Here in this blog we will implement the menu driven program using two different methods :

  • List
  • Dictionary

Let’s Get Started

First let’s build a function which takes input from user :

Now let’s build 4 functions which will perform basic calculations like this :

Now We want a menu to display so let’s use multiline print :

After that take input from user that what operation he wants to perform :

Now we have menu, functions and input available with us, now we need to call that function which user wants

Using List

So we will store the functions inside  a list like this : 

So here we have store the function names inside a list, and you will notice that we are not putting brackets after any function name,
it means we are not calling any function here we are just passing the function reference inside list, because if you call functions by putting brackets in front of the function name, then all the functions will be executed immediately.
So don’t do like this : 

Now we are ready to finally call that function which user wants, so all you need to do is :

What we are doing here is :

  • Pass user choice inside operations[], so that my list will fetch only that function which user has entered inside choice variable, but why we did choice – 1, because index starts from 0 and if user will enter choice 1 to perform addition, then my list will take this as index 1 and call subtract function instead of add.
  • Operations[choice – 1] will return the name of the function that user wants to call and then we put brackets in front of that function and pass the 2 inputs as an arguments.
  • Now in return we will get some output and we stored that inside output variable.

Now run the program and test it…

Now what will happen, if we test this code with wrong choice  :

So now let’s talk about disadvantages of using list :

Problem with list is that if we pass invalid choice then it will lead to error, list index out of range. So you have to use if else here to get rid of this error.

So now we will talk about dictionaries, which is more recommended option in menu driven programs

So here we will store the function names inside a dictionary and bind the userchoice with function name,where userchoice will become the key and function name becomes value for it.

Using Dictionary

Let’s create a dictionary first :

Now we are ready to finally caSo that’s how you create a dictionary and set key and values pairs. After that we use a get() method of dictionary to pass our choice and in return we will get the value as a function that we want to call.ll that function which user wants, so all you need to do is :

So if we pass any choice here then in return operations.get(choice) will return the name of the function assigned to that particular key and immediately call it because we put () at the end of the statement

Now here comes the major advantage of dictionary that what if user passes an invalid choice then how dictionary is going to handle it. So inside get() method we can pass two arguments like :

dict.get(key, default=None)

So if key is valid then the corresponding value will be returned and if key is invalid then you can set a default parameter like this :
Dict.get(key, default = someFunction)

So now we will create a function known as errorHandler which will be called by default when you pass any invalid key. It will return output “Invalid Choice”.

So if we write dict1.get(1),What will it gives?

So this is our errorHandler function which will be called by default if we pass any invalid choice. If you know Switch Case in any programming then there is a default case which is called when no other case is being executed. So in python we can do it by creating a new function and passing this function name in default parameter of dict.get() method. So finally our output variable will look like this :

Lets run it with valid choice first:

Now run it with invalid choice : 

So this was the introduction to use of switch case in python. You can create your own switch case in python. There is no such restrictions. This was one of the way that we use when we want to have any menu driven program.

Sign Up and Start Learning