+ 1

Is there "switch case" structure in Python?

9th Oct 2019, 4:26 AM
APURVA JAISWAL
APURVA JAISWAL - avatar
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’
9th Oct 2019, 5:40 AM
Neha Gupta
Neha Gupta - avatar
0
Thanks Neha Gupta
22nd Oct 2019, 3:51 AM
APURVA JAISWAL
APURVA JAISWAL - avatar