# Switch Case in Python (Replacement)

PythonServer Side ProgrammingProgramming

In this tutorial, we are going to write a program that acts as a switch case in Python.

Other programming languages like C, C++, Java, etc.., have switch whereas Python doesn't. Let's write a program that acts as a switch case in Python.

We are going to use the dict data structure for writing the switch case in Python. Let's see the following code.

## Example

Live Demo

# dictionary with keys and values
days = {
0: 'Sunday',
1: 'Monday',
2: 'Tuesday',
3: 'Wednesday',
4: 'Thursday',
5: 'Friday',
6: 'Saturday'
}
# we will use 'get' method to access the days
# if we provide the correct key, then we will get the corresponding value
# if we provide a key that's not in the dictionary, then we will get the defaul
lue
print(days.get(10, 'Bad day'))

## Output

If you run the above code, then you will get the following result.

Sunday
Bad day

We can place anything in place of the values. The default value of get method represents the default keyword in switch cases.

## Conclusion

If you have any doubts in the tutorial, mention them in the comment section.

Published on 11-Jul-2020 08:06:53