Python Match

Python · Control Flow

Theory

The match statement, added in Python 3.10, compares one value against a series of case patterns from top to bottom and runs the block of the first pattern that matches; later cases are skipped. case 200: matches the literal 200, and case _: is the wildcard that matches anything, acting like a default or else branch. If no case matches and there is no wildcard, nothing happens and no error is raised.

match is more than a switch: it does structural pattern matching. case 401 | 403: matches either value, case (x, y): unpacks a two-item tuple into x and y, case {'status': s}: pulls a value out of a dict, and a guard like case n if n >= 500: adds a condition. One important trap: a bare name in a case is a capture pattern, not a comparison. case code: matches every value and stores it in code; to compare against a constant, use a literal or a dotted name such as Status.OK. Interpreters older than 3.10 need if/elif instead.

Why we use it

Code that branches on the shape or exact value of data, such as status codes, commands or parsed JSON messages, becomes a long if/elif ladder. match states each case clearly and can unpack values as it matches.

When to use it

Use match for many distinct literal values or when you need to destructure tuples, lists or dicts. For two or three simple conditions, or ranges like salary > 80000, if/elif is simpler. On Python versions before 3.10, use if/elif or a dict lookup.

How it works

  1. match status_code:
  2. Does it match case 200? Yes: Run that block: print('OK'). No: Try the next case.
  3. Does it match case 404? Yes: Run that block. No: Fall through to case _.
  4. case _ matches anything: print('Error')
  5. Only the first matching case runs; continue after match

Syntax

match value:
    case pattern1:
        ...
    case _:
        ...

Example

match status_code:
    case 200:
        print('OK')
    case _:
        print('Error')

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic