+ 1
Is there "switch case" structure in Python?
2 Answers
+ 4
We can use a dictionary to map cases to their functionality. Here, we define a function week() to tell us which day a certain day of the week is. A switcher is a dictionary that performs this mapping.
>>> def week(i):
switcher={
0:'Sunday',
1:'Monday',
2:'Tuesday',
3:'Wednesday',
4:'Thursday',
5:'Friday',
6:'Saturday'
}
return switcher.get(i,"Invalid day of week")
Now, we make calls to week() with different values.
Example :
>>> week(2)
âTuesdayâ
>>> week(0)
âSundayâ
>>> week(7)
âInvalid day of weekâ
>>> week(4.5)
âInvalid day of weekâ
0
Thanks Neha Gupta