Why doesn’t Python have switch-case?


Switch-case is a common feature in many programming languages that allows for efficient execution of code based on a variable’s value. So why doesn’t Python, one of the most popular programming languages, have this feature? Let’s take a look.

One of the main reasons why Python doesn’t have switch-case is because it is not considered to be very “Pythonic.” What this means is that the language was designed to encourage code that is readable and maintainable over code that is concise and efficient. In the opinion of the Python development team, switch-case statements do not meet these criteria.

Another reason why switch-case is not supported in Python is because it can be implemented in other ways that are more “Pythonic.” For example, if you want to execute code based on the value of a variable, you can use a dictionary instead. Dictionaries are a built-in data structure in Python that allow you to map keys to values. This makes them ideal for storing associations between values and the code that should be executed when those values are encountered.

The match case Statement in Python (Update: Python 3.10 got match-case syntax in 2021)

Python’s match-case statement is a lot like a switch statement in other programming languages. But, unlike a switch statement, the match-case statement can operate on any type of data, not just integers and strings. This makes the match-case statement more versatile and powerful. In this blog post, we’ll take a look at how the match-case statement works and some of the ways you can use it in your Python programs.

If you’ve ever written a program in a language like C or Java, you’re probably familiar with the switch statement. The switch statement lets you select one of several code paths to execute based on the value of an integer or string variable. Python has a similar construct called the match-case statement. But, unlike the switch statement, the match-case statement can operate on any type of data, not just integers and strings. This makes the match-case statement more versatile and powerful. In this blog post, we’ll take a look at how the match-case Statement works and some of the ways you can use it in your Python programs.

Here is the generic syntax

match subject: case <pattern_01>: <action_01> case <pattern_02>: <action_02> case <pattern_03>: <action_03> case _: <action_wildcard>
Code language: HTML, XML (xml)

and here is a simple example

def http_error(status): match status: case 400: return "Bad request" case 404: return "Not found" case 418: return "I'm a teapot" case _: return "Something's wrong with the Internet"
Code language: JavaScript (javascript)

You can use this as a switch case in Python, and if the conditions don’t match, it will return default.

def func(a): return { 01 : 'output for case 01', 02 : 'output for case 02', 03 : 'output for case 03' }.get(a, 'default case')
Code language: JavaScript (javascript)

Read more here

Although switch-case statements are commonly used in other programming languages, they are not supported in Python. This is because they are not considered to be “Pythonic” and can be implemented in other ways that are more idiomatic to the language.

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts