Updated June 26, 2023
Introduction to Python Switch Statement
A switch statement is a very useful and powerful programming feature. It is an alternate to if-else-if ladder statement and provides better performance and more manageable code than an if-else-if ladder statement. Most of the programming languages (like Java, C, etc.) offer switch statements, but the Python language does not have any switch statements. However, if you like to switch-case statements, there is a very efficient way to implement a switch case statement feature in Python. This tutorial will help you implement it.
Flowchart
The switch statement in every programming language has the same functionality but slightly different syntax. Python language does not provide any inbuilt switch statements. We can implement this feature with the same flow and functionality but with different syntax and implementation using Python Dictionary.
Syntax of Switch Statement
Just looks at the below two different syntax of a switch statement. The first statement shows the standard syntax of switch statements in other programming languages like C, Java, etc. The second statement is the Python implementation of the Switch statement. You can clearly relate how both are similar in functionality but different in implementation.
1. Switch in Other Languages (c, Java,..)
Syntax:
switch(N)
{
case 1: Statement if N = 1;
break;
case 2: Statement if N = 2;
break;
::
case n: Statement if N = n;
break;
default: Statement if N doesn't match any
}
2. Switch Implementation in Python
Syntax:
switcher = {
key_1: value_1/method_1(),
key_2: value_2/method_2(),
key_3: value_3/method_3(),
::
key_n: value_n/method_n(),
}
key = N
value = switcher.get(key, "default"
Working of Switch Statement
Let’s see the working of a switch statement, which are explained below:
- The working of the standard switch statement in any programming language is similar. It consists of multiple cases, each having a code block and break statement.
- The input argument is compared with multiple cases one after another. If the argument matched with any of the cases, then the corresponding block statement is executed, and the control comes out of the switch statement at the break statement.
- If none of the case matches with the input argument, then the default block statement is executed.
- In Python implementation of switch statements, we consider Python dictionary as the switch and keys of the dictionary as cases. Here we call the get() function of the dictionary with required arguments, i.e., input key and a default value.
- If the input key matches any of the dictionary’s keys, then the corresponding value is returned. However, if the input key does not match with any of the dictionary keys, then the default value will be returned.
Implementation of a Switch statement in Python
As we know, the switch case statement and if-else-if ladder statement are alternate to each other. Both can be used to achieve the same purpose, but a switch case statement provides better performance and more manageable code than an if-else-if ladder statement. Python language does not have any switch statements, but we can implement a similar functionality using Python Dictionary.
In the following section, we will see the implementation of each with an example.
Example #1
Use of if-else-if ladder – An alternate to switch case statement. In this example, we have used the if-else-if ladder statement to get the day of the week. For the same use case, we will also see the implementation of switch state in Python.
Code:
def get_week_day(argument):
if(argument == 0):
day="Sunday"
elif(argument == 1):
day="Monday"
elif(argument == 2):
day="Tuesday"
elif(argument == 3):
day="Wednesday"
elif(argument == 4):
day="Thursday"
elif(argument == 5):
day="Friday"
elif(argument == 6):
day="Saturday"
else:
day="Invalid day"
return day
# Driver program
if __name__ == "__main__":
print (get_week_day(6))
print (get_week_day(8))
print (get_week_day(0))
Output:
Example #2
Implementation of switch statement using Dictionary of Strings. Python language does not have any switch statements. However, we can efficiently implement a switch statement feature in Python using Dictionary as below.
Explanation: Dictionary in Python is an unordered collection of key and value pairs. In the below code snippet, the switcher is a dictionary variable that is holding key: value pairs similar to switch-like cases.
Using the get() method, we can access the item of the dictionary by passing the arguments as the key and the optional default value. The default value helps to implement the ‘default case’ of a switch statement.
Code:
def get_week_day(argument):
switcher = {
0: "Sunday",
1: "Monday",
2: "Tuesday",
3: "Wednesday",
4: "Thursday",
5: "Friday",
6: "Saturday"
}
return switcher.get(argument, "Invalid day")
# Driver program
if __name__ == "__main__":
print (get_week_day(6))
print (get_week_day(8))
print (get_week_day(0))
Output:
In this example, let us pass 6 as an input argument or input key, which will be searched in the keys of the dictionary (switcher). As the key=6 is found, then it will return the value as Saturday. Now pass 8 as input key, which will be searched in the keys of the dictionary (switcher). As the key=8 is not found, then it will return the default value as an Invalid day.
Example #3
Implementation of switch statement using Dictionary mapping of functions. In the above example, the values of the dictionary are of string data type, i.e. constant. But It is interesting to know that the values of a Python dictionary can be of any data type.
The values of a Python dictionary can be constants (integers, strings) as well as functions. In this example, we will implement the switch statement, which will also support each switch case’s function call by using the dictionary of functions as below.
Code:
def get_week_day_fun(argument):
def zero():
return "Sunday"
def one():
return "Monday"
def two():
return "Tuesday"
def three():
return "Wednesday"
def four():
return "Thursday"
def five():
return "Friday"
def six():
return "Saturday"
switcher = {
0: zero(),
1: one(),
2: two(),
3: three(),
4: four(),
5: five(),
6: six()
}
return switcher.get(argument, "Invalid day")
# Driver program
if __name__ == "__main__":
print (get_week_day_fun(6))
print (get_week_day_fun(8))
print (get_week_day_fun(0))
Output:
Conclusion
As we have seen above, the same use case can be implemented by both the switch case statement and the if-else-if ladder statement. But Switch’s statement provides a more manageable and readable code than the if-else-if ladder statement. We have also seen that Python language does not have any switch statements, but we can implement the same in a more efficient way using Python Dictionary.
Recommended Articles
This is a guide to Python Switch Statement. Here we discuss syntax, flowchart, and working of python switch statement along with examples and implementation. You may also look at the following articles to learn more-