Python Switch Case
What is a Switch Case Statement?
In the programming languages, a switch statement is a logical loop or syntax that tests the variable's value and compares it with multiple cases. Once the match is found with the cases given in the function, the block statement which is associated with that particular variable will be executed.
- Each case in the given switch statement inside the block has a different number or name.
- The number or name of the case in the switch statement is referred to as the identifier of the case.
- When a user provides value through input, the switch statement compares this value with all of its cases inside the block.
- The process of matching the value with the case in the switch statement runs until the match is found.
- In case when no match is found with the value given by the user, the switch statement will give us a default output.
Note: A switch case statement in any Programming language is a type of selection control which allows the user to let their input variables to change the control flow of the program and execution via searching and mapping.
Python doesn't support the switch case statement:
Although many popular languages like Java, C, C++, PHP, etc., have an in-built switch case statement, it is very surprising to know that the Python language doesn't support the switch statement.
There is no keyword as 'switch' present inside Python programming language.
Switch case replacement in Python (replacement):
In this section, we will write a program in Python, which will act like a switch statement program works in other programming languages.
Since there is no switch statement is available in Python, so we have to use some Pythonic way to implement syntax same as switch statement in our program. For this, we will use some powerful dictionary mappings that are also known as associative arrays. These associative arrays provide simple one-to-one key-value mappings.
Here is an example of a Python program which is the implementation of the switch statement.
Example: In the following example, we will write separate functions, create a dictionary and then and we will build an input function so that the whole Python code will act as a switch statements that work in other programming languages.
We will follow the following steps to build an implementation of the switch case statement:
Step 1: First, we will start by creating separate functions for each case (representing season) that we want to handle.
def spr(): return ('It is Spring', ) def mer(): return ('It is Summer', ) def aut(): return ('It is Autumn', ) def win(): return ('It is Winter', ) def default(): return 'Your given input represents an Invalid Season!'
Note: Always make sure that there is a default case also while handling cases.
Step 2: The next step for us is to define the dictionary for the defined function and cases.
switch_case = { 1: spr, 2: mer, 3: aut, 4: win }
Step 3: The last step for us is to create a function that will take input as an integer from the user and run through all the cases we have defined. The integer function value will automatically invoke the corresponding case function after the dictionary hooks up.
def switch_input(x): return switch_case.get(x, default)()
Now the complete Python program ready with switch case implementation will look like the given code:
def spr(): return ('It is Spring', ) def mer(): return ('It is Summer', ) def aut(): return ('It is Autumn', ) def win(): return ('It is Winter', ) def default(): return 'Your given input represents an Invalid Season!' switch_case = { 1: spr, 2: mer, 3: aut, 4: win } def switch_input(x): return switch_case.get(x, default)()
Now, let's add output variable a in this statement so that the program will give us desirable output. We will give two input let's values to run our program.
print (switch_input(2)) print (switch_input(8))
Now, the final program will look something like this:
def spr(): return ('It is Spring', ) def mer(): return ('It is Summer', ) def aut(): return ('It is Autumn', ) def win(): return ('It is Winter', ) def default(): return 'Your given input represents an Invalid Season!' switch_case = { 1: spr, 2: mer, 3: aut, 4: win } def switch_input(x): return switch_case.get(x, default)() print (switch_input(2)) print (switch_input(8))
When we run the program, we will get the following output:
('It is Summer',) Your given input represents an Invalid Season!
That's how we can implement a program to use it as a switch case statement in Python programming.