0Pricing
Python Academy · Lesson

match and case Basics

Branch on value patterns.

Structural Pattern Matching

Python 3.10 introduced structural pattern matching with the match statement. It compares a value against a series of patterns and runs the first one that matches, far more powerful than chained if/elif.

Basic Syntax

Write match followed by the subject, then one or more case blocks. The first matching case runs.

command = 'start'

match command:
    case 'start':
        print('Starting up')
    case 'stop':
        print('Shutting down')

# prints: Starting up

All lessons in this course

  1. match and case Basics
  2. Matching Sequences and Mappings
  3. Class Patterns
  4. Guards and Wildcards
← Back to Python Academy