All Courses

Switch Case in Python

Pallavi Dhotre

2 years ago

Switch Case in Python | insideAIML
Table of Content
  • Introduction
  • Python switch case statement
  • Using dictionary
  • Using dictionary mapping with functions
  • Summary


          Are you stuck with implementing switch cases in Python? Want to know about the implementation of the python switch case statement, for example? Unlike other programming languages, Python does not support switch statements, but we can achieve this in different ways. This article is about the python switch case statement. But before that, let's understand normal switch case syntax and example:
switch(Boolean expression) {
  case 1:
    // code block1
  case 2:
    // code block2
    // code block
          You can see that keyword switch and case are used as a part of a program while using switch case statements in other programming languages. But this syntax is not valid in Python as it does not provide support for a switch case statement. So this can be achieved by using dictionaries in Python.

Python switch case statement

Using Dictionary

          One way to implement a python switch case is to use dictionaries in Python. As we know that in dictionaries, key-value pairs are stored and using keys, we can access the values associated with them. Keys in python dictionaries work as a case in a switch-case statement. In the switch case statement, we can access the functions or the python strings or values by one element only, which is similar to the key in the dictionary. So for implementing this mechanism in Python, we use a python dictionary in the given way. See the following python switch case example:
#python switch case example using dictionary value string
def language(i):
    switch_demo = {
        0: 'Python',
        1: 'Java',
        2: 'C++',
        3: 'Ruby',
        4: 'C'
    print( switch_demo.get(i, "Invalid language"))

inp=int(input("Enter your choice: "))
Enter your choice: 0
          In this example, we have defined a function language(). Inside the function, we have created a dictionary. We are passing strings as a value to the dictionary. Then we have used a get() method to access the function. In the end, we make a function call by passing an argument to the function. 

Using Dictionary Mapping with Functions

          In python value of a dictionary can have any type, so we can pass the functions in value. See the following code to understand the python switch case syntax and example 
#python switch case example using dictionary
def Python():
    print("I like Python")
def Java():
    print("I like Java")
def CPP():
    print("I like CPP")
def Ruby():
    print("I like Ruby")
def C():
    print("I like C")

def language(i):
    switch_demo = {
        0: Python,
        1: Java,
        2: CPP,
        3: Ruby,
        4: C
    obj=switch_demo.get(i, lambda :"Invalid language")()
    return (obj)
i=int(input("Enter your choice: "))
Enter your choice: 3

I like Ruby
          In this example, we have defined the functions first. Then in another function, we have created a dictionary, and the function name is passed as a value in a dictionary. Then we have used the get() method and lambda() function to access the elements of the function. In the last step, we make a function call and passed an argument.


          In this article, we discussed the python switch case statement and the python switch case example. We conclude that Python does not provide support for switch-case statements directly. We can achieve this mechanism by using the dictionary in Python. We hope you enjoyed the article. If you have any related queries, feel free to ask in the comment section below. 
Like the Blog, then Share it with your friends and colleagues to make this AI community stronger. 
To learn more about nuances of Artificial Intelligence, Python Programming, Deep Learning, Data Science and Machine Learning, visit our insideAIML blog page 
Keep Learning. Keep Growing.

Submit Review