Python switch case statement

60 / 100

Python-switch-case statement

Switch case statement is a one of the most powerful programming feature that allows you to control the flow of your program based on the value of a variable or an expression. You can use it to execute different blocks of code, depending on the variable value during runtime. Here’s an example of a switch statement in Python.

This is Python way to implement switch statement is to use the powerful dictionary mappings, also known as associative arrays, that provide simple one-to-one key-value mappings.

Here’s the Python implementation of the above switch statement. In the following example, we create a dictionary named switcher to store all the switch-like cases.

def switch_example(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")
In the above example, when you pass an argument to the switch_example function, it is looked up against the switcher dictionary mapping. If a match is found, the associated value is printed, else a default string (‘Invalid Month’) is printed. The default string helps implement the ‘default case’ of a switch statement.

Dictionary mapping for functions

Here’s where it gets more interesting. The values of a Python dictionary can be of any data type. So you don’t have to confine yourself to using constants (integers, strings), you can also use function names and lambdas as values.

For example, you can also implement the above switch statement by creating a dictionary of function names as values. In this case, switcher is a dictionary of function names, and not strings.

def one():
    return "January"
def two():
    return "February"
def three():
    return "March"
def four():
    return "April"
def five():
    return "May"
def six():
    return "June"
def seven():
    return "July"
def eight():
    return "August"
def nine():
    return "September"
def ten():
    return "October"
def eleven():
    return "November"
def twelve():
    return "December"

def numbers_to_months(argument):
    switcher = {
        1: one,
        2: two,
        3: three,
        4: four,
        5: five,
        6: six,
        7: seven,
        8: eight,
        9: nine,
        10: ten,
        11: eleven,
        12: twelve
    # Get the function from switcher dictionary
    func = switcher.get(argument, lambda: "Invalid month")
    # Execute the function
    print func()

Although the above functions are quite simple and only return strings, you can use this approach to execute elaborate blocks of code within each function.

Learn more about python in detail:

  1. Basics of Python
  2. Python Data types
  3. Python Variables
  4. Python Loops

Python switch case statement post reference:


%d bloggers like this: