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
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 Python
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 sta
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 t
1 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: S
4 min read
Statement, Indentation and Comment in Python Here, we will discuss Statements in Python, Indentation in Python, and Comments in Python. We will also discuss different rules and examples for Python Statement, Python Indentation, Python Comment, and the Difference Between 'Docstrings' and 'Multi-line Comments. What is Statement in Python A Pytho
7 min read
Python String istitle() Method The istitle() method in Python is used to check whether a string follows the title case formatting. In a title-cased string, the first letter of each word is capitalized, and all other letters in the word are in lowercase. This method is especially useful when working with formatted text such as tit
3 min read
Python If Else Statements - Conditional Statements In Python, If-Else is a fundamental conditional statement used for decision-making in programming. If...Else statement allows to execution of specific blocks of code depending on the condition is True or False.if Statementif statement is the most simple decision-making statement. If the condition ev
4 min read
Alternatives to Case/Switch Statements in Python In many programming languages, the case or switch statement is a control flow mechanism that allows a variable to be tested for equality against a list of values, with each value associated with a block of code to be executed. However, Python does not have a built-in case or switch statement. Instea
3 min read
Check multiple conditions in if statement - Python If-else conditional statement is used in Python when a situation leads to two conditions and one of them should hold true. Syntax:if (condition): code1else: code2[on_true] if [expression] else [on_false]Note: For more information, refer to Decision Making in Python (if , if..else, Nested if, if-elif
4 min read
Python - if , if..else, Nested if, if-elif statements There are situations in real life when we need to do some specific task and based on some specific conditions, we decide what we should do next. Similarly, there comes a situation in programming where a specific task is to be performed if a specific condition is True. In such cases, conditional stat
7 min read