Implementing Switch Case In Python

Implementing Switch Case In Python

Hey buddy 👋, welcome back to my blog. Are you a Pythonista and have ever wondered how you can implement a switch or case statement in Python. Well, there are many workarounds you can get to do to reach your goal.

Unlike most programming languages, Python does not have a switch or case statement. To get around this fact, we shall use dictionary mapping in this article.

What is a Switch or Case Statement❓

A case or switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via a multiway branch.

The switch-case statement is a powerful programming feature that allows you to control the flow of your program based on the value of a variable or an expression.

else if vs switch

image.png

The fundamental difference between if-else and switch statements is that the if-else statement “selects the execution of the statements based upon the evaluation of the expression in if statements”.

The switch statements “selects the execution of the statement often according to a keyboard command”.

Read More : Tech Differences

Pros of using the Switch Case Statement:

🎈 It is easier to debug.
🎈 It is easier to read the program by anyone except the programmer.
🎈 It is easier to understand and also easier to maintain.
🎈 It is easier to verify all values that are to be checked are handled.

Cons of using the Switch Case Statement:

🎈 May lead to code smell
🎈 You can not use the variable expression in case.
🎈 You cannot use the same constant in two different cases.

Switch Case Implementation In Javascript

let day;
switch (new Date().getDay()) {
  case 0:
    day = "Sunday";
    break;
  case 1:
    day = "Monday";
    break;
  case 2:
    day = "Tuesday";
    break;
  case 3:
    day = "Wednesday";
    break;
  case 4:
    day = "Thursday";
    break;
  case 5:
    day = "Friday";
    break;
  case  6:
    day = "Saturday";
}

Let's implement the above in Python:

def Sunday():
    return "Sunday"
def Monday():
    return "Monday"
def Tuesday():
    return "Tuesday"
def Wednesday():
    return "Wednesday"
def Thursday():
    return "Thursday"
def Friday():
    return "Friday"
def Saturday():
    return "Saturday"

# The mapping
switcher = {
    0: Sunday,
    1: Monday,
    2: Tuesday,
    3: Wednesday,
    4: Thursday,
    5: Friday,
    6: Saturday,
    }

# The call
def switch(dayOfWeek):
    return switcher.get(dayOfWeek)()

# The test
print(switch(3))

The Output: Since Today(at time of writing) ->Wednesday

In a nutshell: A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.

🔸 Github Repo:
🔸 Youtube Video:

Conclusion

As you can see, the python implementation seems longer this time which may result in code smell but at the end of the day, you have what you want.

You can as well use if-elif-else and classes in Python to achieve the same thing: You could also import datetime module to make closely simulate the above JS code.

ToDo: Try adding a default statement in the python code to catch errors:
Hint: Python dict.get() accepts two arguments

If you enjoyed this article, consider subscribing, sharing and reacting to it.

Stay Tuned!

Ronnnie Atuhaire 😎