Python is well known for its easy syntax and flexible inbuilt data structures and methods. But it still misses some of the basic data structures and syntax offered by other static high-level programming languages. Python does not support switch case, which provides an alternative and cleaner way to write a control flow program like
if-else
statement. You can find switch case syntax in every popular static programming language, including Java, C++, Ruby, and C, but there is no such built-in concept as "Python Switch Case".
However, you can use some ways and techniques to mimic the working of switch case for Python, and here in this article, we have mentioned some of the popular techniques you can use in Python to implement a data flow like switch case.
Switch-case in Python
No, Python does not support the Switch Case Statement. A Switch Case statement is a part of the Data Flow concept like if, and if-else. Generally, the switch-case statement is divided into two parts, the switch, and case. The switch contains multiple cases, and each case specifies a value, and according to the variable which satisfies one of the case values, the switch execute that case statement.
The switch case's working flow is pretty similar to the
if-else
statement, but switch case provides a cleaner way to implement multiple choice statements.
How to implement switch-case in Python?
Only those familiar with other programming languages like C, C++ or Java may do not need to know how a switch statement works or how we can easily implement it using Python if-elif statement . You can also follow other techniques to implement a Python Switch case statement, using Python dictionary , and Python classes . Before discovering all the different techniques to implement a switch case in Python, let’s look at a C++ switch case statement code.
int weekday;
cin>>weekday;
switch(weekday)
{
case 1:
cout<<"Its Sunday";
break;
case 2:
cout<<"Its Monday";
break;
case 3:
cout<<"Its Tuesday";
break;
case 4:
cout<<"Its Wednesday";
break;
case 5:
cout<<"Its Thursday";
break;
case 6:
cout<<"Its Friday";
break;
case 7:
cout<<"Its Saturday";
break;
default:
cout<<"Incorrect day";
}
Implement Python Switch Case using Dictionary Mapping
Dictionary is a built-in data structure of Python that uses key-value pairs to store data objects. To implement a switch-case statement using a dictionary, we use dictionary name as a switch, its keys as cases, and values as case statements.
def sunday():
return "It's Sunday"
def monday():
return "It's Monday"
def tuesday():
return "It's Tuesday"
def wednesday():
return "It's Wednesday"
def thursday():
return "It's Thursday"
def friday():
return "It's Friday"
def saturday():
return "It's Saturday"
switcher ={ 1:sunday(),
2:monday()
3:tuesday(),
4:wednesday(),
5:thursday(),
6:friday(),
7:saturday()
}
def switch(weekday):
try:
return switcher[weekday]
except:
#this will act as a default statement
return "incorrect weekday"
print(switch(1))
print(switch(2))
print(switch(7))
print(switch(10))
Output
It's Sunday
It's Monday
It's Saturday
incorrect weekday
Implement Python Switch Case Using Python Classes
It’s very easy to implement a switch case expression using Python classes. Inside the class we define 7 methods for 7 different cases, and a switch method to invoke one of those cases according to the user entered weekday.
class PythonSwitch:
def case_1(self):
return "It's Sunday"
def case_2(self):
return "It's Monday"
def case_3(self):
return "It's Tuesday"
def case_4(self):
return "It's Wednesday"
def case_5(self):
return "It's Thursday"
def case_6(self):
return "It's Friday"
def case_7(self):
return "It's Saturday"
def switch(self, wd):
if wd in range(1,8):
return eval(f"self.case_{wd}()")
else:
return "Incorrect weekday"
switch = PythonSwitch()
print(switch.switch(1))
print(switch.switch(4))
print(switch.switch(10))
Output
It's Sunday
It's Wednesday
Incorrect weekday
Implement Python Switch Case using Python If-elif
It’s very straightforward to implement a switch-case like expression using Python if-elif statements. The example below uses the Python function and Python if-elif statement to create switch case statement.
def switch(case):
if case==1:
return "It's Sunday"
elif case==2:
return "It's Monday"
elif case==3:
return "It's Tuesday"
elif case==4:
return "It's Wednesday"
elif case==5:
return "It's Thursday"
elif case==6:
return "It's Friday"
elif case==7:
return "It's Saturday"
else:
return "incorrect weekday"
print(switch(1))
print(switch(4))
print(switch(10))
Output
It's Sunday
It's Wednesday
incorrect weekday
Conclusion
In this Python tutorial article, you learned that python does not support switch case like other programming languages. You also learned the different techniques to implement switch case in Python.
If you want to know why python does not support switch case please check this official documentation PEP3103 . If you like this article or have any query related to the above programs feel free to comment down below.
People are also reading:
Leave a Comment on this Post