Since switch-case is not used in python – what are the replacements for switch statement in python?



Since switch-case is not used in python – what are the replacements for switch statement in py..

Answer / Mukul Bansal

In Python, there's no direct equivalent to the switch-case statement found in languages like C++ or Java. However, you can achieve similar functionality using a series of if-elif statements or dictionaries.nnHere's an example using dictionaries:nn```pythonndef my_function(x):n lookup = {'apple': 1, 'banana': 2, 'cherry': 3}n return lookup.get(x, 'Invalid fruit') # returns the value associated with x if it exists in the dictionary, otherwise it returns 'Invalid fruit'```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Python Interview Questions

Is all the memory freed when python exits?

1 Answers  


What is tkinter?

1 Answers  


What’s your preferred text editor?

1 Answers  


What is self?

1 Answers  


How do you parse a json file in python?

1 Answers  


What is the output of print str[4: ] if str = ‘ python language’?

1 Answers  


What is a coverage report?

1 Answers  


How do you extend in python?

1 Answers  


Is print a keyword in python?

1 Answers  


What is python distutils?

1 Answers  


How lists is differentiated from tuples?

1 Answers  


What does join () do in python?

1 Answers  


Categories