Using A Switch Statement in Python

Written by ashishnair1211 | Published 2020/10/15
Tech Story Tags: python-programming | python | python-tutorials | python-tips | python-developers | learn-python | clean-code | coding

TLDR There is no switch statement in Python. Python's dictionary object can be used as a switch statement. The implementation is very easy and intuitive to use. The Pythonic answer to the switch statement is easy to write using Python's Dictionary object. The solution is to use Python's switch statement to deal with multiple conditions in a control flow statement. It is more convenient and feels a lot cleaner to write and structure the code using this approach. The added benefit is, it looks COOL! The solution to this problem is the Pythonic solution to it.via the TL;DR App

Yes, you read that right. If you have been coding for a while and if Python is not the first programming language that you started with, then you definitely know what a Switch statement is, and appreciate how flawless it is, when you need to factor in multiple conditions/cases for a control flow.
But if you are just starting out or are using Python as the first language, there is a chance that you don't know about the Switch statement and don't know how smooth it is to code a control flow statement using it.
This is how a switch statement looks like in Java,
switch (variable/expression) {
case value1:
   // statements of case1
   break;

case value2:
   // statements of case2
   break;

   .. .. ...
   .. .. ...

default:
   // default statements
}
You use the parenthesis to pass a variable into the switch statement and you define cases, which are just different conditions, the variable that you pass to the switch statement is compared with all of these cases and depending on which case condition is satisfied, that case gets executed.
The default block is what is executed when the variable doesn't satisfy any of the given case conditions.
Yeah, I know what you might be thinking by now. What's the big deal you could just use the
if-elif-else
statement instead?
Sure, you could but when dealing with multiple conditions the switch statement is more convenient and feels a lot cleaner.
So, now to the main part, How to get the switch statement in Python ?
Well to be honest, there is no switch statement in Python. But wait, dont click the close button just yet. I have a workaround to this problem.

Using the Python Dictionary as a Switch statement

So here it is, the Pythonic answer to the switch statement. Python's dictionary object can be used as a switch statement and the implementation is very easy and feels intuitive to use.
I will show you how to write a switch statement using Python with an example.
Suppose you want to write a function
month_tracker()
, that takes in the current month as an integer and returns the name of the month.
If you use
if-elif-else
statement this is how your code would look like;
def month_tracker(month):
    if month == 1:
       month_name = 'January'
    elif month == 2:
       month_name = 'February'
    elif month == 3:
       month_name = 'March'
    elif month == 4:
       month_name = 'April'
    elif month == 5:
       month_name = 'May'
    elif month == 6:
       month_name = 'June'
    elif month == 7:
       month_name = 'July'
    elif month == 8:
       month_name = 'August'
    elif month == 9:
       month_name = 'September'
    elif month == 10:
       month_name = 'October'
    elif month == 11:
       month_name = 'November'
    elif month == 12:
       month_name = 'December'
    else :
       month_name = 'January'
    return month_name
And if we did it the switch statement-esque way,
def month_tracker(month):
    switch = {
        1 : 'January',
        2 : 'February',
        3 :'March',
        4 :'April',
        5 :'May',
        6 :'June',
        7 :'July',
        8 :'August',
        9 :'September',
        10 :'October',
        11 :'November',
        12 :'December',
    }
    return switch.get(month,1)
See, what I was talking about, see how good and clean the second approach looks.
How easy is it to write and structure the code using this approach ?
And the added benefit is, it looks COOL!
Go ahead and try it out on your next program.

Peace!


Written by ashishnair1211 | I develop software for Python and Android.
Published by HackerNoon on 2020/10/15