What Is the Python Match Case Statement and Why Use It?
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:
1 2 3 4 5 6 7 8 9 10 11 12 |
match parameter case pattern1: code to execute for pattern1 case pattern2: code to execute for pattern2 … … case pattern10 code to execute for pattern10 case _: default code to execute |
Let’s start with our X = 5 example. That code would look like this:
1 2 3 4 5 6 7 8 9 |
x = '5' match x: case '5': print('X equals 5') case '10': print('X does not equal 5') case other: print('I have no idea what X is') |
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:
1 2 3 4 5 6 7 8 9 |
command = 'Hello, New Stack!' match command: case 'Hello, New Stack!': print('Hello, Jack') case 'Hello, World!': print('Hello to everyone!') case other: print('Goodbye') |
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:
1 |
def numMatch(): |
Next, we have our input statement that asks the user to enter a number between 1 and 5. That line looks like this:
1 |
num = int(input("Enter a number between 1 and 5: ")) |
We now create our match case statement which looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
match num: case 1: print('You typed 1') case 2: print('You typed 2') case 3: print('You typed 3') case 4: print('You typed 4') case 5: print('You typed 5') case _: print('Your number is not between 1 and 5') |
We finally execute our function with:
1 |
numMach() |
The entire code looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
def numMatch(): num = int(input("Enter a number between 1 and 5: ")) match num: case 1: print('You typed 1') case 2: print('You typed 2') case 3: print('You typed 3') case 4: print('You typed 4') case 5: print('You typed 5') case _: print('Your number is not between 1 and 5') numMach() |
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:
1 |
case 1 | 2: |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
def numMatch(): num = int(input("Enter a number between 1 and 10: ")) match num: case 1 | 2: print('You typed 1 or 2') case 3 | 4: print('You typed 3 or 4') case 5 | 6: print('You typed 5 or 6') case 7 | 8: print('You typed 7 or 8') case 9 | 10: print('You typed 9 or 10') case _: print('Your number is not between 1 and 10') numMatch() |
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.