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_suite

Parameters & Attributes

NameTypeDescription
subjectexpressionEvaluated once for matching.
patternpatternStructure or value to test.
guardexpressionOptional test after successful match.
suitestatement suiteSelected case body.

Working Example

Example Snippetpython
subject=('ok',7)match subject:    case ('ok',value):        result=value    case _:        result=0print(result)
Python Reference