---
source: https://iinventedlight.com/library/notes/python-structural-pattern-matching.md
title: "Python match statements compare a subject against case patterns"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs (tutorial)); Public domain or CC0-1.0 (Python Enhancement Proposals); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Python docs (tutorial), More Control Flow Tools: match statements, PSF-2.0 (https://raw.githubusercontent.com/python/cpython/main/LICENSE); Python Enhancement Proposals, PEP 636 – Structural Pattern Matching: Tutorial, Public domain or CC0-1.0 (https://peps.python.org/pep-0636/#copyright)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-16
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-16
as_of: 2026-11-16
confidence: medium
untested: "Not reproduced (2026-11-16). Python tutorial and PEP 636 read; examples were not executed."
category: coding-references
topic_area: "code-python: structural pattern matching"
tags: [python, match, case, pattern-matching, pep-636]
client: Librarian
sources:
  - id: S1
    title: "More Control Flow Tools: match statements"
    publisher: Python docs (tutorial)
    url: https://docs.python.org/3/tutorial/controlflow.html#match-statements
    accessed: 2026-11-16
    license: "PSF-2.0"
  - id: S2
    title: "PEP 636 – Structural Pattern Matching: Tutorial"
    publisher: Python Enhancement Proposals
    url: https://peps.python.org/pep-0636/
    accessed: 2026-11-16
    license: "Public domain or CC0-1.0"
---
# Python match statements compare a subject against case patterns
**Answer:** A `match` statement compares the value of an expression with successive `case` patterns. Only the first matching pattern runs. If no pattern matches, no branch is executed.

## Pattern forms

The simplest form compares a subject value against literals; combine several literals in a single pattern using `|`. The `_` pattern is a wildcard that never fails. Sequence patterns bind variables from a subject value, similar to unpacking assignment. Class patterns can capture attributes into variables.

## Guards

A guard is the keyword `if` followed by an expression. The guard is part of the case, not the pattern. The guard runs only after its pattern matches and pattern variables are bound.

## Background

PEP 636 is tutorial material for the pattern matching introduced by PEP 634.

## See also

- [rust-pattern-syntax](https://iinventedlight.com/library/notes/rust-pattern-syntax/)

## Claims

- id: C1
  claim: Only the first matching pattern runs.
  source: S1
  evidence: "Only the first pattern that matches gets executed"

- id: C2
  claim: The underscore acts as a wildcard.
  source: S1
  evidence: "acts as a *wildcard* and never fails to match."

- id: C3
  claim: Guards are part of the case.
  source: S2
  evidence: "The guard is not part of the pattern, it's part of the case."

- id: C4
  claim: PEP 636 is a tutorial for PEP 634.
  source: S2
  evidence: "This PEP is a tutorial for the pattern matching introduced by"

## Sources

- [S1] More Control Flow Tools: match statements. Python docs (tutorial). https://docs.python.org/3/tutorial/controlflow.html#match-statements . License: PSF-2.0. Accessed 2026-11-16.

- [S2] PEP 636 – Structural Pattern Matching: Tutorial. Python Enhancement Proposals. https://peps.python.org/pep-0636/ . License: Public domain or CC0-1.0. Accessed 2026-11-16.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs (tutorial), More Control Flow Tools: match statements, PSF-2.0 (https://raw.githubusercontent.com/python/cpython/main/LICENSE); Python Enhancement Proposals, PEP 636 – Structural Pattern Matching: Tutorial, Public domain or CC0-1.0 (https://peps.python.org/pep-0636/#copyright). Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
