1# Here is one way to implement a switch construct
2# Switcher is a dictionary data type here
3def week(i):
4 switcher={
5 0:'Sunday',
6 1:'Monday',
7 2:'Tuesday',
8 3:'Wednesday',
9 4:'Thursday',
10 5:'Friday',
11 6:'Saturday'
12 }
13 return switcher.get(i,"Invalid day of week")
14
15print(week(5)) # Call the function
1def switch_demo(argument):
2 switcher = {
3 1: "January",
4 2: "February",
5 3: "March",
6 4: "April",
7 5: "May",
8 6: "June",
9 7: "July",
10 8: "August",
11 9: "September",
12 10: "October",
13 11: "November",
14 12: "December"
15 }
16 print switcher.get(argument, "Invalid month")
17
1# example for a Python 3.10.x match...case block
2string = input("Enter a letter: ")
3
4match string:
5 case "a":
6 print("You entered a!")
7 case "b":
8 print("You entered b!")
9 case _: # this is for unknown/wildcard inputs
10 print(f"Unknown input: {string!r}")