- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Switch Case in Python (Replacement)
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
# 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(0, 'Bad day')) 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.
- Related Articles
- Case-insensitive string replacement using Python Program
- Switch case in Arduino
- Switch case statement in C
- Switch case calculator in JavaScript
- String in Switch Case in Java
- How to use case-insensitive switch-case in JavaScript?
- The String in Switch Case in Java
- What is Switch...case statement in JavaScript?
- Explain nested switch case in C language
- Using range in switch case in C/C++
- How to implement switch-case statement in Kotlin?
- Please explain what happens when PHP switch case executes case 0?
- How to come out of a switch case in JavaScript?
- How to use an enum with switch case in Java?\n
- Converting digits to word format using switch case in C language

Advertisements