Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Const

case

case introduces a pattern-matching block inside Python’s match statement. Cases are checked in order, can bind matched values, and may include guards or a wildcard fallback.

Since
3.10

How it works

case introduces an individual pattern-and-action block inside a match statement. Python tests cases in source order, optionally evaluates a guard after a pattern succeeds, and runs the first successful case; a wildcard can provide a fallback.

Syntax

case patterns [guard] : suite

Key points

  • Each case supplies the pattern against which the value provided to the enclosing match statement is tested.
  • Patterns can be composed from smaller subpatterns, enabling nested structural checks.
  • A successful pattern can bind portions of the subject to names for use in that case's suite.
  • The case form consists of the keyword, a pattern, an optional guard, a colon, and an indented suite.

Common mistakes

  • case is a soft keyword, so it remains usable as an identifier in contexts where the parser does not interpret it as part of pattern matching.
  • Without a matching case or wildcard fallback, the match statement performs no case action; provide _ when an unmatched input needs explicit handling.
  • A case block may include an optional guard expression after its pattern, allowing additional conditions beyond structural pattern matching.

Example: Matching mapping shapes

def describe(value):
    match value:
        case {"status": "ok", "data": data}:
            return f"success: {data}"
        case {"status": "error", "message": message}:
            return f"failure: {message}"
        case _:
            return "unknown"

print(describe({"status": "ok", "data": 42}))

This function matches mapping shapes, binds selected values, and uses a wildcard for all other inputs.

When to use it

Structural dispatch

Dispatch based on the shape or contents of dictionaries, sequences, or objects.

Destructuring input

Extract fields from a matched value while selecting the handling branch.

Guarded cases

Add a boolean condition after a structural pattern succeeds.

Interview questions

QWhat is the role of case in Python pattern matching?

case defines a pattern block within match. The pattern is compared with the subject, and the block's suite runs when matching succeeds.

Follow-up: How are multiple cases selected? Python checks them from top to bottom and executes the first successful case.

QHow does a guard affect a case block?

A case can contain a pattern and an optional guard. The guard is evaluated after the pattern matches, and the suite runs only when that guard is true.

Follow-up: What happens when the guard is false? Python continues considering later case blocks.

QHow do you handle unmatched subjects?

case _: is the wildcard fallback. It matches when no earlier pattern succeeds, so it is useful for handling remaining input shapes.

Follow-up: Is the wildcard mandatory? No; without it, an unmatched subject simply leaves the match without selecting a case.

  • match statement
  • patterns
  • guards
  • wildcard pattern `_`
  • soft keywords

Source: 8. Compound statements. Checked 2026-10-16.