Python match Statement
match is the structured pattern matching statement introduced in Python 3.10, similar to the switch-casein other languages, but more powerful.
The match statement can match patterns of data structures, not only constant values but also complex patterns such as types, sequences and dictionaries.
Word meaning: match means "match" and is used for pattern matching.
Basic syntax and parameters
The match statement is a new feature in Python 3.10+, used to replace complex multi-branch if-elif structures.
Syntax format
match 变量:
case 模式1:
代码块1
case 模式2:
代码块2
case _:
默认代码块
Pattern types
- Constant pattern: Matches a specific value.
- Wildcard pattern:
case _Matches any value, equivalent to default. - Type pattern: Matches a data type.
- Sequence pattern: Matches sequences such as lists and tuples.
- Dictionary pattern: Matches dictionaries.
- Pattern with a condition: Use
ifto add an extra condition.
Example
Example 1: basic usage (similar to switch)
Example
Requires Python version 3.10+
def
http_status
(
status
)
:
match status:
case
200
:
return
"OK"
case
404
:
return
"Not Found"
case
500
:
return
"Server Error"
case _:
return
"Unknown"
(
http_status
(
200
)
)
Output: OK
(
http_status
(
404
)
)
Output: Not Found
(
http_status
(
999
)
)
Output: Unknown
Expected output:
OK
Not Found
Unknown
Code analysis:
case _is a wildcard that matches all unmatched cases.- Similar to default in switch-case.
Example 2: matching multiple values
Example
Multiple values match the same result
def
color_name
(
code
)
:
match
code
:
case
"r"
|
"R"
:
return
"Red"
case
"g"
|
"G"
:
return
"Green"
case
"b"
|
"B"
:
return
"Blue"
case _:
return
"File %s type is: %s%n"
(
color_name
(
"r"
)
)
Output: Red
(
color_name
(
"G"
)
)
Output: Green
(
color_name
(
"x"
)
)
Output: Unknown
[
/mycode
]
<
/div
<
/div
<
p
<
strong
Expected output:
<
/strong
<
/p
<
pre
Red
Green
Unknown
<
/pre
<
p
Use
<
code
|
<
/code
Multiple values can be combined into the same case.
<
/p
<
h3
Example
3
: sequence pattern matching
<
/h3
<
div
class
=
"example"
<
h2
class
=
"example"
Example
<
/h2
<
div
class
=
"example_code"
[
mycode4
type
=
"python"
]
Sequence pattern matching
def
describe_point
(
point
)
:
match point:
case
(
0
,
0
)
:
return
"Origin"
case
(
x
,
0
)
:
return
f
"Point on the X axis ({x}, 0)"
case
(
0
,
y
)
:
return
f
"Point on the Y axis (0, {y})"
case
(
x
,
y
)
:
return
f
"Point on the plane ({x}, {y})"
case _:
return
"Invalid coordinates"
(
describe_point
(
(
0
,
0
)
)
)
Output: Origin
(
describe_point
(
(
5
,
0
)
)
)
Output: Point on the X axis (5, 0)
(
describe_point
(
(
3
,
4
)
)
)
Output: Point on the plane (3, 4)
Expected output:
原点
X轴上的点 (5, 0)
平面上的点 (3, 4)
match can destructure tuples/lists and extract the values inside them.
Example 4: dictionary pattern matching
Example
Dictionary pattern matching
def
process_user
(
user
)
:
match
user
:
case
{
"name"
: name
,
"age"
: age
}
:
return
f
"User: {name}, age: {age}"
case
{
"name"
: name
}
:
return
f
"User: {name}"
case _:
return
"Invalid user"
(
process_user
(
{
"name"
:
"Tom"
,
"age"
:
20
}
)
)
(
process_user
(
{
"name"
:
"Jerry"
}
)
)
(
process_user
(
{
"role"
:
"admin"
}
)
)
Expected output:
用户: Tom, 年龄: 20
用户: Jerry
无效用户
Dictionary patterns can extract the values of specific keys.
Example 5: patterns with conditions
Example
Pattern matching with conditions
def
classify_number
(
n
)
:
match n:
case n
if
n
0
:
return
f
"Positive number ({n})"
case n
if
n
<
0
:
return
f
"Negative number ({n})"
case
0
:
return
"Zero"
(
classify_number
(
10
)
)
Output: Positive number (10)
(
classify_number
(
5
)
)
Output: Negative number (-5)
(
classify_number
(
0
)
)
Output: Zero
Complex conditions
def
describe_list
(
lst
)
:
match lst:
case
[
]
:
return
"Empty list"
case
[
x
]
:
return
f
"Single element: {x}"
case
[
x
,
y
]
:
return
f
"Two elements: {x}, {y}"
case
[
x
,
*rest
]
:
return
f
"First: {x}, rest: {rest}"
(
describe_list
(
[
]
)
)
(
describe_list
(
[
1
]
)
)
(
describe_list
(
[
1
,
2
,
3
,
4
]
)
)
Expected output:
正数 (10)
负数 (-5)
零
空列表
单个元素: 1
第一个: 1, 其余: [2, 3, 4]
Use case 变量 if 条件 Additional matching conditions can be added.
Python3 Conditional Control