The world’s leading publication for data science, AI, and ML professionals.

Python Now Supports Switch Statements – Here’s How to Get Started

Learn how to implement Switch statements in Python 3.10 with Structural Pattern Matching

Photo by Robert Wiedemann on Unsplash
Photo by Robert Wiedemann on Unsplash

Python 3.10 is still in alpha, but will bring along some new exciting features. We’ll look at one of these today – switch statements – officially known as Structural Pattern Matching.

Switch statements are commonly seen in most Programming languages and provide a neater way of implementing conditional logic. They come in handy when there’s a lot of conditions to evaluate.

Today we’ll see how to use them and compare the code differences with a more traditional approach.

The article is structured as follows:

  • The Old Way(s) of Implementing Conditional Operations
  • Python 3.10 Way – Switch Statement
  • Final Thoughts

The Old Way(s) of Implementing Conditional Operations

We’ll cover two. The first one is your standard if-elif-else statement, and the other will use the dictionary key-value mappings to avoid if statements altogether.

First things first, we need some code. We’ll declare a function called get_mood(day: str) -> str that returns a string whose value depends on the input parameter. The returned value gets more exciting as we approach the weekend. Silly little function, but will do for demonstration purposes.

Let’s implement it with a classical if methodology. Here’s the code:

Nothing new or groundbreaking here. The code is simple to understand but is too verbose, especially when multiple values can satisfy a single condition.

We can "improve" or mix things up a bit by avoiding if statements altogether and writing the function in the form of key-value mappings. This also includes a try except block to set up a default return value.

Here’s the code snippet:

As you can see, the results are identical, but we haven’t used conditional operators. Both approaches will work fine, but what Python was always lacking was a dedicated switch statement.

Version 3.10 fixes that.


Python 3.10 Way – Switch Statement

According to the official documentation:

Structural pattern matching has been added in the form of a match statement and case statements of patterns with associated actions. Patterns consist of sequences, mappings, primitive data types as well as class instances. Pattern matching enables programs to extract information from complex data types, branch on the structure of data, and apply specific actions based on different forms of data.

We’ll stick to the basics today and explore everything structural pattern matching has to offer some other time.

Let’s circle back to our get_mood() function and rewrite it with a switch statement-like syntax. Unlike many other programming languages, Python uses the match keyword instead of a switch. The case keyword is identical.

Here’s the code snippet:

To recap:

  • Use the case keyword to evaluate for a condition (case 'Monday' is identical to if day == 'Monday')
  • Separate multiple conditions with the pipe operator – | – e.g., if two input values should result in the same return value
  • Use the underline operator – _ – to specify the default case.

And there’s your structural pattern matching – at least the basics of it. It’s just one of the exciting new features coming soon in Python 3.10.


Final Thoughts

Python 3.10 will bring many exciting features, but it’s still in alpha. Since it’s not production-ready, it might not be a good idea to install it as your default Python version.

Today we’ve covered possibly the most exciting new feature, but the others will also get covered in the upcoming articles. Stay tuned to the blog to learn more.


Loved the article? Become a Medium member to continue learning without limits. I’ll receive a portion of your membership fee if you use the following link, with no extra cost to you.

Join Medium with my referral link – Dario Radečić


Learn More


Stay Connected


Related Articles