Python Match Case Statement
Introduced in Python 3.10, the match case statement offers a powerful mechanism for pattern matching in Python. It allows us to perform more expressive and readable conditional checks. Unlike traditional if-elif-else chains, which can become unwieldy with complex conditions, the match-case statement provides a more elegant and flexible solution.
Example:
def check_number(x):
match x:
case 10:
print("It's 10")
case 20:
print("It's 20")
case _:
print("It's neither 10 nor 20")
check_number(10)
check_number(30)
Explanation:
- In this example, the function check_number(x) uses a match-case statement to compare the value of x to the constants 10 and 20.
- If x equals 10, it prints “It’s 10”. If x equals 20, it prints “It’s 20”.
- If neither condition is met, the wildcard _ matches any value, leading to the message “It’s neither 10 nor 20”.
Let’s take a look at python match case statement in detail:
Table of Content
- Python Match Case Statement Syntax
- Using Match-Case with Different Data Types
- Match Case Statements with Constants
- Match Case Statement with OR Operator
- Match Case Statement with Python If Condition
- Match Case Statement on Sequences
- Match Case Statement on Mappings (Dictionaries)
- Match Case Statement with Python Class
Python Match Case Statement Syntax
The match-case syntax is based on structural pattern matching, which enables matching against data structures like sequences, mappings and even classes, providing more granularity and flexibility in handling various conditions. This feature is particularly useful when dealing with different types of data in a clear and organized manner.
match subject:
case pattern1:
# Code block if pattern1 matches
case pattern2:
# Code block if pattern2 matches
case _:
# Default case (wildcard) if no other pattern matches
- match subject: The value (or variable) to match against.
- case pattern: A pattern to match the subject.
- _ (Wildcard): A default catch-all pattern, similar to a “default” in other languages’ switch statements.
Now, let us see a few examples to know how the match case statement works in Python.
The power of the match-case statement lies in its ability to match a variety of data types, including constants, sequences, mappings and custom classes. Let’s explore how to use match-case with different data types.
Match Case Statements with Constants
Matching constants is one of the simplest uses of the match-case statement. It checks if a variable matches specific constant values and allows for different actions based on the matched constant.
def greet(person):
match person:
case "A":
print("Hello, A!")
case "B":
print("Hello, B!")
case _:
print("Hello, stranger!")
greet("A")
greet("B")
Explanation:
- The function greet(person) checks the value of person using the match-case statement.
- It specifically matches “A” and “B”, printing a personalized greeting.
- If the value of person doesn’t match any of the constants, the wildcard _ matches any value and prints “Hello, stranger!”.
Match Case Statement with OR Operator
The match-case statement can also be used with logical operators like or to combine multiple patterns. This allows for a more flexible matching system where you can group patterns and check for a match against any of them.
Example:
def num_check(x):
match x:
case 10 | 20 | 30: # Matches 10, 20, or 30
print(f"Matched: {x}")
case _:
print("No match found")
num_check(10)
num_check(20)
num_check(25)
Explanation:
- The pattern 10 | 20 | 30 uses the or operator to match any of these three values. If x equals 10, 20 or 30, the case will execute.
- If x doesn’t match any of the values, the wildcard _ catches it and prints “No match found”.
Match Case Statement with Python If Condition
We can also add an if condition after a case to create more granular control over the matching process. This allows us to add additional checks within a case.
Example:
def num_check(x):
match x:
case 10 if x % 2 == 0: # Match 10 only if it's even
print("Matched 10 and it's even!")
case 10:
print("Matched 10, but it's not even.")
case _:
print("No match found")
num_check(10)
num_check(15)
Explanation:
- The first case matches 10 but only if x % 2 == 0. If this condition is met, it prints a specific message.
- The second case matches 10 without the condition, ensuring that a fallback message is shown if the first case isn’t executed.
Match Case Statement on Sequences
The match-case statement is particularly powerful when working with sequences such as lists or tuples. We can match individual elements of a sequence or even match the structure of the sequence itself.
Example:
def process(data):
match data:
case [x, y]:
# A list with two elements
print(f"Two-element list: {x}, {y}")
case [x, y, z]:
# A list with three elements
print(f"Three-element list: {x}, {y}, {z}")
case _:
print("Unknown data format")
process([1, 2])
process([1, 2, 3])
process([1, 2, 3, 4])
Explanation:
- The function process_data(data) matches the structure of the list data.
- The first case matches if the list has exactly two elements, binding the first and second elements to x and y respectively.
- The second case matches if the list has exactly three elements, binding them to x, y and z.
- If the list does not match either pattern, the wildcard _ is used to print “Unknown data format”.
Match Case Statement on Mappings (Dictionaries)
A mapping is another common data type in Python and match-case can be used to match against dictionaries, checking for specific keys and values.
def person(person):
match person:
# Dictionary with name and age keys
case {"name": name, "age": age}:
print(f"Name: {name}, Age: {age}")
# Dictionary with only name key
case {"name": name}:
print(f"Name: {name}")
case _:
print("Unknown format")
person({"name": "Alice", "age": 25})
person({"name": "Bob"})
person({"city": "New York"})
Explanation:
- The function person(person) matches the structure of a dictionary person.
- The first case matches a dictionary that contains both “name” and “age” keys, binding their corresponding values to the variables name and age.
- The second case matches a dictionary with just the “name” key.
- The wildcard _ is used to catch any other dictionary structure that doesn’t match these patterns, printing “Unknown format”.
Match Case Statement with Python Class
One of the most powerful features of match-case is the ability to match against classes. We can match instances of a class and even extract specific attributes of an object for further handling.
Example:
class Shape:
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def check_shape(shape):
match shape:
# Match Circle and extract the radius
case Circle(radius):
print(f"circle radius {radius}.")
# Match Rectangle and extract width and height
case Rectangle(width, height):
print(f"Rectangle width {width} and height {height}.")
# Default case for any other object
case _:
print("This is an unknown shape.")
# Create objects of Circle and Rectangle
circle = Circle(10)
rectangle = Rectangle(4, 6)
# Test with different shapes
check_shape(circle)
check_shape(rectangle)
Explanation:
- We have a Shape class and two types of shapes: Circle and Rectangle.
- The check_shape function checks if the shape is a Circle or Rectangle and prints their details.
- If the shape isn’t one of these, it says “unknown shape.”
Python Match Case Statement – FAQs
What is the match case statement in Python?
The match-case statement, also known as pattern matching, is a feature introduced in Python 3.10. It provides a concise and expressive way to perform pattern matching on data structures, such as tuples, lists and classes. It allows you to match the value of an expression against a series of patterns and execute the corresponding block of code for the matched pattern.
How does the match case statement differ from if-elif-else statements?
The match-case statement is a more powerful and expressive construct compared to if-elif-else statements. While if-elif-else statements rely on boolean expressions, match-case statements can match patterns based on the structure and value of the data. Match-case statements provide a more structured and readable way to handle multiple conditions and perform different actions based on those conditions.
What are the benefits of using the match-case statement?
The match-case statement offers several benefits, including:
- Conciseness: Match-case statements allow you to express complex branching logic in a concise and readable manner.
- Readability: Pattern matching makes the code more readable and self-explanatory, as it closely resembles the problem domain.
- Safety: Match-case statements provide exhaustive pattern matching, ensuring that all possible cases are handled.
How to do match case in Python?
To perform match case in Python, you can use the match statement, introduced in Python 3.10. It allows pattern matching with case clauses. Here’s a basic example:
def match_example(value):
match value:
case 1:
return "The value is 1"
case 2:
return "The value is 2"
case _:
return "The value is something else"
print(match_example(1)) # Output: The value is 1
print(match_example(3)) # Output: The value is something else
Is match case the same as switch case?
Match case in Python is similar to switch case in other languages, but it supports more complex patterns and expressions, not just constant values. It offers greater flexibility and readability in handling multiple conditions.