Switch case is a case selection method in any programming language. In which an element is matched with a collection of a similar type of element. If a match is found then performed if not then a default action is performed.
Switch case in all programming language has same structure as:
Syntax:
function(element){
case1(match1): action1
break;
case2(match2): action2
break;
Default(): action
}
Switch case with match-case:
With python keyword, match-case Switch case can be implemented.
Syntax:
def function(element):
match element:
Case1 match1 : action1
Case2 match2 : action2
Case3 match3 : action
Code:
def fun(element):
match element:
case "1":
print("One")
case "2":
print("Two")
case "3":
print("Three")
case _:
print("No match")
# Initialization of variable x
x = "2"
print("Number is", fun(x))
Output:
Switch case with if else:
Switch case can be implemented with help of an if-else ladder.Each case is implemented inside the condition of each if case.
Syntax:
def function(element):
if(condition1) :
action1
elseif(condition2):
action2
else :
action3
Code:
def fun(ele):
if x=='a':
print("a")
elif x=='b':
print("b")
elif x=="c":
print("c")
else:
print("NO Match")
x='b'
print("char is:")
fun(x)
Output:
Switch case with the dictionary:
Switch cases can be implemented with the dictionary data type.All the cases are implemented inside the dictionary. Cases are matched with the keys of the dictionary.
Syntax:
dictionary = {
match1 : action1,
match2 : action2,
match3 : action3
}
def function(element):
return dictionary.get(element,”NO Match”)
Code:
def a():
return "a"
def b():
return "b"
def c():
return "c"
def default():
return "No match"
dic = {
'a':a,
'b':b,
'c':c
}
def fun(ele):
return dic.get(ele,default)()
x='b'
print("Char is:")
print(fun(x))
Output:
Switch case with python lambda:
Switch can be implemented with the lambda in python. It is mostly used in case we have to implement dynamic functionality in the switch case. The action of the case changes as per input given.
Code:
def fun(oper,element):
result={
'+':lambda element: element+2,
'-':lambda element: element-2,
'*':lambda element: element*2,
'/':lambda element: element/2
}
return result.get(oper,"NO match")(element)
x='+'
y=3
print("3+2")
print(fun(x,y))
Output:
Switch case with classes:
Switch case can be implemented with the classes and getattr function.
Code:
class fun:
def f(self,element):
default = "NO match"
return getattr(self,'case_'+str(element),lambda: default)()
def case_a(self):
return "char is a"
def case_b(self):
return "char is b"
def case_c(self):
return "char is c"
obj=fun()
print(obj.f('c'))
Output:
Conclusion:
We hope this article has given you a crisp idea on how to use of match-case keywords in python to implement switch cases (as we know in other programming languages). Four ways to implement switch cases with if-else ladder, dictionary, python lambda, and classes.
Match case is easier and straight-forward to use in Python compared to if-else ladder. Therefore, we use a python switch statement as it makes code easy, fast to develop, increase readability, and easy to find bugs as compared to if else ladder.