Since switch-case is not used in python – what are the replacements for switch statement in python?
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 |
Is all the memory freed when python exits?
What is tkinter?
What’s your preferred text editor?
What is self?
How do you parse a json file in python?
What is the output of print str[4: ] if str = ‘ python language’?
What is a coverage report?
How do you extend in python?
Is print a keyword in python?
What is python distutils?
How lists is differentiated from tuples?
What does join () do in python?