AME AME - 8 months ago 26
Python Question

Does Python have an equivalent to 'switch'?

I am trying to check each index in an 8 digit binary string. If it is

then it is
otherwise its

Is there a more concise way to write this code with a switch-like feature.?


No it doesn't. In the Python core language, one of the rules is to only have one way to do something. The switch is redundant to:

if x == 1:
elif x == 5:
elif x == 10:

(without the fall-through, of course).

The switch was originally introduced as a compiler optimization for C. Modern compilers no longer need these hints to optimize this sort of logic statement.