The latest versions of Python does support switch case statement in the form of match case statement or you can also use a dictionary as a replacement for switch case statement in Python as shown in below example –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
## Version 3.10 or later def myfun(x): match x: case 'a': return 1 case 'b': return 2 case _: return 0 # 0 is the default case if x is not found ## Older version def myfun(x): return { 'a': 1, 'b': 2 }.get(x, 0) # 0 is the default case if x is not found myfun('a') ## Returns 1 myfun('z') ## Returns 0 |