Ram
0
Q:

switch case in python

string week(i){
       switch(i){
               case 0:
                       return “Sunday”
                       break;
               case 1:
                       return “Monday”
                       break;
               case 2:
                       return “Tuesday”
                       break;
               case 3:
                       return “Wednesday”
                       break;
               case 4:
                       return “Thursday”
                       break;
               case 5:
                       return “Friday”
                       break;
               case 6:
                       return “Saturday”
                       break;
               default:
                       return “Invalid day of week”
       }
}
2
# Here is one way to implement a switch construct
# Switcher is a dictionary data type here
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 week")

print(week(5)) # Call the function
17
// switch statement 
switch(expression)
{
   // case statements
   // values must be of same type of expression
   case value1 :
      // Statements
      break; // break is optional
   
   case value2 :
      // Statements
      break; // break is optional
   
   // We can have any number of case statements
   // below is default statement, used when none of the cases is true. 
   // No break is needed in the default case.
   default : 
      // Statements
}
25
def switch_demo(argument):
    switcher = {
        1: "January",
        2: "February",
        3: "March",
        4: "April",
        5: "May",
        6: "June",
        7: "July",
        8: "August",
        9: "September",
        10: "October",
        11: "November",
        12: "December"
    }
    print switcher.get(argument, "Invalid month")
4
def week(i):
        switcher={
                0:'Sunday',
                1:'Monday',
                2:'Tuesday',
                3:'Wednesday',
                4:'Thursday',
                5:'Friday',
                6:'Saturday'
             }
         return switcher.get(i,"Choose b/w 0-7")
0

New to Communities?

Join the community