How to Use match case Statement in Python
Hello everyone, Let’s learn about python match statement.All of us know that Python match statement is a pattern matching statement with a syntax of sub-expressions for:
- Matching the shape of a sequence
- Matching items in a sequence
- Matching keys and values in a dictionary
- Matching literal values
In Python, if…else or match statements can be used to verify decisions. Why is match even necessary in Python when we can use if..else for decision-making? However, if you write code that checks many condition statements simultaneously, you cannot use the if…else statement since it will make your code very bulky and difficult to read. In Python, we use match in place of the if…else statement when checking multiple condition statements.
The basic match statement
thing = 2
match thing:
case 1:
print("thing is 1")
case 2:
print("thing is 2")
case 3:
print("thing is 3")
case _:
print("thing is not 1, 2 or 3")
First, let’s look at a basic example
This is the basic syntax for a Python match statement. If you are unaware of the unusual wild card scenario for the match statement known as “_”, let me explain. It can be compared to the else statement because it matches any value. A slightly more useful example is to automatically store the result when it doesn’t match. In this example I uses an underscore (_), which is not actually stored in _ because it is a special case, but if we
name the variable differently, we can store the result, here one example
Matching from variables
You can do this with the match statement as well, in this code, we’re trying to match one variable with another variable.
Matching multiple values in a single case
As you can see, using the “ | ” operator (which is also used for bitwise operations), you can test for multiple values at the same time.This roughly means that if variable is either equal to LEFT or UP, print the “left — up”, and same for second case also. If variable is either equal to dOWN or RIGHT, print the “right -down”.
For Extra Conditions
Let’s say you have one scenario where you want a more advanced comparison such as ( if number > 45 ). you can do this type of advance comparison with the “match” statement with the help of the “ guards” feature of it. let’s learn this with an example.
In this case, I’ve declared a single variable called num1 and given it the value 12. Currently, I’m checking whether num1 is less than 10 to print this, and between 10 and 20 to print that. You might be wondering what the line “case num if num < 10” means. In this line, I define the variable num and check to see if it is less than 10. the value of num1 will indirectly be passed into num, it means any variable you define inside a case will have the same value as the match variable and it will then determine if the value is less than 10. If it is the case, only “less than 10” will be printed.
this is another example
Matching lists, tuples, and other sequences
If you are familiar with tuple unpacking, you can probably guess how sequence matching works:
The first case explicitly matches both of the given values, which is identical to if value == (0, 1):. The second case explicitly matches 0 for the first value, but leaves the second value as a variable and stores it in y. Effectively this comes down to if value[0] == 0: y = value[1]. The last case stores a variable for both the x and y values and will match any sequence with exactly two items.
“Mastering Python Second Edition by Rick van Hattem” is the name of the book that I advise every Python developer to read.
Thankyou