Language Syntax
Match — Python syntax
Evaluate the subject once and try cases in order. Execute the first successful pattern with a truthy guard. Matching can bind names; an unmatched subject executes no case. Added in 3.10.
Formal Syntax & Structure
python syntaxpython
match subject: case pattern if guard: suite case _: fallback_suiteParameters & Attributes
| Name | Type | Description |
|---|---|---|
| subject | expression | Evaluated once for matching. |
| pattern | pattern | Structure or value to test. |
| guard | expression | Optional test after successful match. |
| suite | statement suite | Selected case body. |
Working Example
Example Snippetpython
subject=('ok',7)match subject: case ('ok',value): result=value case _: result=0print(result)