TNS
VOXPOP
404 - VOXPOP NOT FOUND
Give use a minute to figure out what's going on here ...
Python / Software Development

What Is the Python Match Case Statement and Why Use It?

This tutorial will show you how to use the match case statement, which was introduced in Python 3.10
Aug 8th, 2024 5:00pm by
Featued image for: What Is the Python Match Case Statement and Why Use It?
Featured image via Unsplash+.

When Python 3.10 was released, it included a very important new feature called the match case statement. What this does is give you even more control over the flow of your programs. With match case statements, you control what parts of code are executed if/when conditions (aka cases) are met.

Before we continue, understand that, because the match case statement was introduced in Python 3.10, you need to be running that version or newer to use this feature.

Back to the statement at hand.

Here’s how it works.

Let’s say you define x as 5. You could then create match case statements that would print out different things based on the number. For example, you could have the following match case statements:

  • If X is 5, print X equals 5
  • If X is 10, print X does not equal 5
  • Otherwise, print I have no idea what X is

Of course, the above is not the syntax of a match case statement, but we’ll get to that.

Essentially, what you do is define X (whatever X is) and then compare different cases against the variable. If a case matches, the program executes whatever is associated with the match (in this case it would print X equals 5).

This sounds similar to if-elif-else statements, so why would you opt to go the match case route? Simply put, it makes it easier to simplify flow control. By doing this your code is not just easier to read but easier to debug. Any time you can achieve those two things it’s a win.

But how do you actually use match case statements?

The syntax of the match case statement looks like this:


Let’s start with our X = 5 example. That code would look like this:


If you were to run the above code, the output would be:

X equals 5

We can do the same thing with a Hello, World! app like this:


The output of the above command would be: Hello, Jack.

Let’s make this a bit more useful. Instead of defining X as a hard-coded variable, let’s accept user input. We’ll do this by first defining a function like this:


Next, we have our input statement that asks the user to enter a number between 1 and 5. That line looks like this:


We now create our match case statement which looks like this:


We finally execute our function with:


The entire code looks like this:


When you run the above, you’ll be asked to type a number between 1 and 5 and the app will print out the number you typed. If you type any number outside of those parameters, the app will print out: Your number is not between 1 and 5.

You can also use OR operators with match case statements. For the case statement, you would use something like:


This means if the case is either 1 or 2, print what follows. Change the entire block of code from above to use the OR operator and it looks like this:


Another way to use match case statements is with an if condition. Let’s allow our user to enter any number (negative or positive). The match case statement will then match the inputted number with a negative, zero, or positive statement. That code looks like this:

Run the above and if the user inputs a negative number, it’ll print Your number is negative. If your user inputs a positive number, it’ll print: Your number is positive. Otherwise, it’ll print: Your number is zero.

And that’s the gist of using the Python match case statement.

Group Created with Sketch.
TNS owner Insight Partners is an investor in: Simply.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.