I want to make a switch/case statement in Python, but I don't know how, can anyone give me an example? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 17

I want to make a switch/case statement in Python, but I don't know how, can anyone give me an example?

Switch statement in Python

24th Dec 2016, 12:06 AM
Gami
Gami - avatar
15 Answers
+ 13
well, what can i replace it with?
24th Dec 2016, 12:09 AM
Gami
Gami - avatar
+ 13
is there anything shorter than if... else?
24th Dec 2016, 12:10 AM
Gami
Gami - avatar
+ 13
thank you visph
24th Dec 2016, 12:22 AM
Gami
Gami - avatar
+ 13
yey thank you guys :)
24th Dec 2016, 12:24 AM
Gami
Gami - avatar
+ 12
Can someone upvote this, so I can get a badge please?
24th Dec 2016, 12:23 AM
Gami
Gami - avatar
+ 4
switch case is not available in python.... To simulate it you can use multiple if statements....
24th Dec 2016, 5:35 AM
Aruj Sharma
Aruj Sharma - avatar
+ 2
switch statement is not available in Python
24th Dec 2016, 12:09 AM
Rishi Anand
Rishi Anand - avatar
+ 2
if... else
24th Dec 2016, 12:10 AM
Rishi Anand
Rishi Anand - avatar
+ 2
no
24th Dec 2016, 12:12 AM
Rishi Anand
Rishi Anand - avatar
+ 2
switch-case is error prone. Nevertheless, in many languages, it can be used for limited types. I think that is why a solid practical language like Python does not bother to provide it. Use good old if-else, you can use it for any complex conditions as well as can use them within hierarchy.
16th Jan 2017, 5:02 AM
sharper_than_learning_curve
- 1
# Gami in regard to alternatives to if else # in python import this my_var = 55 my_var2 = 56 if my_var < my_var2: print('smaller') else: print('equal or larger') # there are other options # to if else but they are not # always as obvious to interpret. # Consider that python's ethos is clarity # if choosing to use them print(('equal or larger','smaller')[my_var < my_var2])
24th Dec 2016, 1:08 AM
richard
- 1
this will give u the basic idea of how to use switch
1st Mar 2017, 6:08 PM
siddharth shekhawat
siddharth shekhawat - avatar
- 1
use can use nested if-else or else if statement s
2nd Mar 2017, 5:25 AM
Vivian Mascarenhas
Vivian Mascarenhas - avatar
- 2
public class Program { public static void main(String[] args) { int day = 3; switch(day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; } } }
1st Mar 2017, 6:08 PM
siddharth shekhawat
siddharth shekhawat - avatar