How to implement a switch-case statement in Python?

Rate this post

Today, We want to share with you switch case in python.In this post we will show you switch statement python, hear for implement Python Switch Case statement we will give you demo and example for implement.In this post, we will learn about Laravel 6 Switch Case Statements with an example.

Python Switch case statement examples

public static void switch_demo(String[] args) {

  int calender = 7;

  String dynamicInfo;

  switch (calender) {

  case 1:
    dynamicInfo = "January";

    break;

  case 2:
    dynamicInfo = "February";

    break;

  case 3:
    dynamicInfo = "March";

    break;

  case 4:
    dynamicInfo = "April";

    break;

  case 5:
    dynamicInfo = "May";

    break;

  case 6:
    dynamicInfo = "June";

    break;

  case 7:
    dynamicInfo = "July";

    break;

  case 8:
    dynamicInfo = "August";

    break;

  case 9:
    dynamicInfo = "September";

    break;

  case 10:
    dynamicInfo = "October";

    break;

  case 11:
    dynamicInfo = "November";

    break;

  case 12:
    dynamicInfo = "December";

    break;

  default:
    dynamicInfo = "Invalid calender";

    break;

  }

  System.out.println(dynamicInfo);

}

Free Live Chat for Any Issue

Example 1 – Switch using an if-else-if ladder

def switch():
    option = int(input("enter your option from 1-3 to get the name on calender : "))

    if option == 1:
        response = "January"
        print("the calender is = ",response)
 
    elif option == 2:
        response =  "febuary"
        print("the calender is ", response)
 
    elif option == 3:
        response = "march"
        print("the calender is ", response)
 
    else:
        print("Incorrect option")
 
 
 
switch()

Example 2:
Switch case statement using class to convert literal to string ‘calender’

class PythonSwitchStatement:
 
    def switch(self, calender):
        default = "Incorrect calender"
        return getattr(self, 'case_' + str(calender), lambda: default)()
 
    def case_1(self):
        return "January"
 
    def case_2(self):
        return "February"
 
    def case_3(self):
        return "March"
 
    def case_4(self):
        return "April"
 
    def case_5(self):
        return "May"
 
    def case_6(self):
        return "June"
    def case_7(self):
        return "July"
 
s = PythonSwitchStatement()
 
print(s.switch(3))
print(s.switch(4))
print(s.switch(10))

Example 3 – Dictionary mapping replacement

def numbers_to_strings(argument):

 switcher = {
0: "zero",
1: "one",
2: "two",
}

 return switcher.get(argument, "nothing")

# Driver program

if __name__ == "__main__":

 argument=1

print(numbers_to_strings(argument))

Example 4 – using a dictionary mapping to return value

b ={
'a' : 8956,
'b' : 5235,
'c' : 4587,
'd' : 6895
}


inp = input('input a character : ')

print('The response for inp is : ', b.get(inp, -1))


Example 5 – Using dictionary mapping to switch the days of the week

def week(i):

 switcher={

0:'Sunday',
1:'Monday',
2:'Tuesday',
3:'Wednesday',
4:'Thursday',
5:'Friday',
6:'Saturday'

}
 return switcher.get(i, "Invalid day of the week")

Download

I hope you get an idea about How Can We Best Switch in Python?.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.