Hit enter after type your search item

The Python switch case statement?

The switch case is a decision-making statement that enables switching on a single variable where that variable may have one or multiple possible values. The switch statement is available in many programming languages including Java, PHP, JavaScript etc.

However, Python has no switch case statement. The proposal for switching on multiple values using switch/case was rejected.

The question is what is the alternative? As such, the scenarios will come where you need the switch case usage as doing programming in Python.

The official site suggests using the if…elif…elif..else statements for the limited number of possibilities (say up to 12 or so). However, for a very large number of possibilities, you may create a dictionary.

In that case, the dictionary will map the case values to functions call. I will show the running example with code in the coming section.

First alternative – using the dictionary

As mentioned in the official documentation, you should use the dictionary mapping case values to functions call.

This is demonstrated in the demo below. The scenario is to get the current month from the system and display a respective message to the screen. As there are only twelve possibilities, so 12 “cases” are created i.e. one function for each month.

The current month value is getting by using the datetime module and this is how the dictionary is mapped with case values:

See online demo and code


switch case statement

So, the curr_Month is assigned the system value for Month by using datetime.date.today().strftime(“%B”). This is passed to the dictionary get method that is mapped to a function call.

 

What if a case value is non-existent – the default statement?

In our example of Python switch case alternative, we used Month value that we got by using the time module. So, there was no possibility that a non-existent case happen.

What if a value does not exist? In case of switch statement, the default case can be used that will execute if the provided value does not match any case.

Suppose, we set the value for curr_Month variable a non-month, see what will happen to the above example:

curr_Month =  ‘No Month’

As you execute the above example with this value, this will be the output:

switch case err

See online demo and code

So, an error occurs. How to avoid this?

Using the default “case”

To avoid the error, you may create a default function that acts as an alternative to the “default case” in the switch context. So if none of the cases matches the provided value, the default function should execute. This is demonstrated in the example below.

A user is asked to enter a number from 1 to 7 by using the input function. The 1 represents Monday, 2 for Tuesday and so on. For any other entry, the default case will execute. See the code and output:

See online demo and code


Python  switch case

In the output figure, you can see the last entry is 11 and it executed the default case rather than producing an error.

Note: the get method of default returns the value for key if key is in the dictionary. Otherwise, it returns default.

Switch alternative 2 – using if..elif..elif..else statement

As per official recommendation, for the small number of possibilities, you may use the if..elif, elif…else statements. In that case, the else acts as the “default” case in the context of the switch statement.

See the above Week example by using this approach:

See online demo and code


switch if elif else

For more on if..elif and else statements, follow the link.

This div height required for enabling the sticky sidebar