In this tutorial, we’ll explain multiple ways to implement the Switch Case In Python
. Before we get started, if you want to Conditional Statements in Python, please go through the following article: Conditional Statements in Python.
Switch Case In Python
Switch case is a powerful decision-making construct commonly used in modular programming. Unlike other programming languages, Python doesn’t provide a switch case instrument over the self. However, it has many other constructs like a dictionary, lambda function, and classes to write a custom implementation of switch case in python.
If you are keen to know why Python doesn’t have a switch case, then do refer the explanation at PEP 3103.
A Typical Switch Case in Java Programming
The switch case statement is a powerful programming feature that allows you to control the flow of your program based on the value of a variable or an expression. You can use it to execute different blocks of code, depending on the variable value during runtime. Here’s an example of a switch statement in Java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
public static void switch_demo(String[] args) { int month = 8; String monthString; switch (month) { case 1: monthString = "January"; break; case 2: monthString = "February"; break; case 3: monthString = "March"; break; case 4: monthString = "April"; break; case 5: monthString = "May"; break; case 6: monthString = "June"; break; case 7: monthString = "July"; break; case 8: monthString = "August"; break; case 9: monthString = "September"; break; case 10: monthString = "October"; break; case 11: monthString = "November"; break; case 12: monthString = "December"; break; default: monthString = "Invalid month"; break; } System.out.println(monthString); } |
Here’s how it works:
- The compiler generates a jump table for a switch case statement
- The switch variable/expression is evaluated once
- The switch statement looks up the evaluated variable/expression in the jump table and directly decides which code block to execute.
- If no match is found, then the code under default case is executed
In the above example, depending on the value of variable month
, a different message will be displayed in the standard output. In this case, since the month=8, ‘August’ will be printed in standard output.
Switch Case using a Dictionary
It is simple to use a dictionary for implementing Switch Case in Python. Follow the below steps.
- First, define individual functions for every case.
- Make sure there is a function/method to handle the default case.
- Next, make a dictionary object and store each of the functions beginning with the 0th index.
- After that, write a switch() function accepting the day of the week as an argument.
- The switch() calls the get() method on the dictionary object which returns the function matching the argument and invokes it simultaneously.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# Implement Python Switch Case Statement using Dictionary def monday(): return "monday" def tuesday(): return "tuesday" def wednesday(): return "wednesday" def thursday(): return "thursday" def friday(): return "friday" def saturday(): return "saturday" def sunday(): return "sunday" def default(): return "Incorrect day" switcher = { 1: monday, 2: tuesday, 3: wednesday, 4: thursday, 5: friday, 6: saturday, 7: sunday } def switch(dayOfWeek): return switcher.get(dayOfWeek, default)() print(switch(1)) print(switch(0)) |
The output is as follows:
1 2 3 4 |
Monday Incorrect day |
Switch Case using a Class
It is quite easy to use a class for implementing a Switch Case in Python. Let’s do it with an example.
- In the below example, there is a PythonSwitch class which defines the switch() method.
- It takes the day of the week as an argument, converts it to a 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.
- If the string doesn’t find a match, then the getattr() returns the lambda function as default.
- The class also has the definition of functions specific to different cases.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# Implement Python Switch Case Statement using Class class PythonSwitch: def switch(self, dayOfWeek): default = "Incorrect day" return getattr(self, 'case_' + str(dayOfWeek), lambda: default)() def case_1(self): return "Monday" def case_2(self): return "Tuesday" def case_3(self): return "Wednesday" s = PythonSwitch() print(s.switch(1)) print(s.switch(0)) |
The output is as follows:
1 2 3 4 |
Monday Incorrect day |
Leave a Comment