Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python match Statement

Python match Statement

Python3 Conditional Statements Python3 Conditional Control


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


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"

print

(

http_status

(

200

)

)

 

Output: OK

print

(

http_status

(

404

)

)

 

Output: Not Found

print

(

http_status

(

999

)

)

 

Output: Unknown

Expected output:

OK
Not Found
Unknown

Code analysis:

  1. case _ is a wildcard that matches all unmatched cases.
  2. 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"

print

(

color_name

(

"r"

)

)

 

Output: Red

print

(

color_name

(

"G"

)

)

 

Output: Green

print

(

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"

print

(

describe_point

(

(

0

,

0

)

)

)

     

Output: Origin

print

(

describe_point

(

(

5

,

0

)

)

)

     

Output: Point on the X axis (5, 0)

print

(

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"

print

(

process_user

(

{

"name"

:

"Tom"

,

"age"

:

20

}

)

)

print

(

process_user

(

{

"name"

:

"Jerry"

}

)

)

print

(

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"

print

(

classify_number

(

10

)

)

 

Output: Positive number (10)

print

(

classify_number

(

5

)

)

   

Output: Negative number (-5)

print

(

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}"

print

(

describe_list

(

[

]

)

)

print

(

describe_list

(

[

1

]

)

)

print

(

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 Statements Python3 Conditional Control