Is there a Python equivalent to the 'switch' statement in Javascript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Is there a Python equivalent to the 'switch' statement in Javascript?

As the question says, is there a way to treat multiple elif statements by using something like the 'switch' statement in Javascript? Thanks, folks.

24th Apr 2017, 4:45 PM
Fungai Makawa
Fungai Makawa - avatar
1 Answer
+ 17
No... But you can use dictionaries instead.. a = 4 dict = {4:5, 6:7} print(dict[a]) is the equivalent in JS switch(a) { case 4: return 5 break case 6: return 7 break default: break } However, if you want something similar to switch, this link might be helpful http://stackoverflow.com/questions/60208/replacements-for-switch-statement-in-JUMP_LINK__&&__python__&&__JUMP_LINK
24th Apr 2017, 5:02 PM
Gami
Gami - avatar