Python Match Case Statement
Last Updated :
18 Dec, 2024
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:
Python
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:
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.
Python
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:
Python
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:
Python
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:
Python
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.
Python
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:
Python
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.”
Similar Reads
Python break statement
The break statement in Python is used to exit or "break" out of a loop (either a for or while loop) prematurely, before the loop has iterated through all its items or reached its condition. When the break statement is executed, the program immediately exits the loop, and the control moves to the nex
5 min read
Python - Multi-Line Statements
In this article, we are going to understand the concept of Multi-Line statements in the Python programming language. Statements in Python: In Python, a statement is a logical command that a Python interpreter can read and carry out. It might be an assignment statement or an expression in Python. Mul
3 min read
Python Continue Statement
Python continue statement is a loop control statement that forces to execute the next iteration of the loop while skipping the rest of the code inside the loop for the current iteration only, i.e. when the continue statement is executed in the loop, the code inside the loop following the continue st
3 min read
Nested-if statement in Python
For more complex decision trees, Python allows for nested if statements where one if statement is placed inside another. This article will explore the concept of nested if statements in Python, providing clarity on how to use them effectively. Python Nested if StatementA nested if statement in Pytho
2 min read
Conditional Statements in Python
Conditional statements in Python are used to execute certain blocks of code based on specific conditions. These statements help control the flow of a program, making it behave differently in different situations. If Conditional Statement in PythonIf statement is the simplest form of a conditional st
6 min read
Python String casefold() Method
Python String casefold() method is used to convert string to lowercase. It is similar to the Python lower() string method, but the case removes all the case distinctions present in a string. Python String casefold() Method Syntax Syntax: string.casefold() Parameters: The casefold() method doesn't ta
1 min read
Python | sympy.has() method
With the help of sympy.has() method, we can check if variable is used in that mathematical expression or not and this will return a boolean values by using sympy.has() method. Syntax : sympy.has(variable) Return : Return boolean value i.e True or False. Example #1 : In this example we can see that b
1 min read
Python string capwords() method
capwords() method in Python is a part of the string module and is used to capitalize the first letter of every word in a given string while converting all other letters to lowercase. To use capwords(), the string module must be imported as it is not a built-in string method. [GFGTABS] Python import
3 min read
Switch Case in Python (Replacement)
In this article, we will try to understand Switch Case in Python (Replacement). What is the replacement of Switch Case in Python?Unlike every other programming language we have used before, Python does not have a switch or case statement. To get around this fact, we use dictionary mapping. Method 1:
5 min read
Python | sympy.compare() method
With the help of sympy.compare() method, we can compare the variables and it will return 3 values i.e -1 for smaller, 0 for equal and 1 for greater by using sympy.compare() method. Syntax : sympy.compare() Return : Return the value of comparison i.e -1, 0, 1. Example #1 : In this example we can see
1 min read