match and case Basics
Branch on value patterns.
match and case Basics is a free Python Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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 upMatching Literals
The simplest patterns are literal values: numbers, strings, True, False, and None. They match when the subject equals the literal.
code = 404
match code:
case 200:
print('OK')
case 404:
print('Not Found')
case 500:
print('Server Error')The Wildcard _
The underscore _ is a wildcard pattern that matches anything. Place it last as a catch-all default, like else.
code = 418
match code:
case 200:
print('OK')
case _:
print('Unknown status')Combining with OR
Use the | operator inside a case to match any of several patterns.
code = 301
match code:
case 200 | 201 | 204:
print('Success')
case 301 | 302:
print('Redirect')
case _:
print('Other')Capturing the Value
A bare name in a pattern captures the subject into that variable. Use it when you matched a catch-all but still want the value.
value = 42
match value:
case 0:
print('zero')
case other:
print('got', other)Order Matters
Cases are tested top to bottom and the first match wins. A broad capture pattern placed too early will shadow later, more specific cases.
x = 5
match x:
case n: # matches everything first!
print('caught by capture')
case 5:
print('this never runs')Match Returns Nothing
match is a statement, not an expression, so it does not return a value. Set a variable inside the cases if you need a result.
day = 6
label = ''
match day:
case 0 | 6:
label = 'weekend'
case _:
label = 'weekday'
print(label)Matching None
You can match None directly. This is cleaner than an explicit is None check inside a chain.
def describe(value):
match value:
case None:
return 'nothing'
case _:
return 'something: ' + str(value)
print(describe(None))
print(describe(7))A Practical Dispatcher
Pattern matching shines as a command dispatcher, turning a long if-chain into a clear table of cases.
def handle(cmd):
match cmd:
case 'help':
return 'Showing help'
case 'quit' | 'exit':
return 'Goodbye'
case _:
return 'Unknown command'
print(handle('exit'))
print(handle('foo'))Matching True, False, and None
The constants True, False, and None are matched by identity, not equality. This makes them reliable singletons to branch on.
def report(flag):
match flag:
case True:
return 'enabled'
case False:
return 'disabled'
case None:
return 'unset'
print(report(True))
print(report(None))Quick Check
What does the wildcard pattern _ do in a match statement?
Recap
You learned to branch on value patterns.
matchtests cases top to bottom; first match wins.- Match literals, use
_as catch-all, combine with|. - A bare name captures the value.
matchis a statement, not an expression.
Frequently asked questions
Is the “match and case Basics” lesson free?
Yes — the full text of “match and case Basics” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.
What will I learn in “match and case Basics”?
Branch on value patterns. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python Academy?
No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “match and case Basics” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python Academy lesson?
Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- match and case Basics
- Matching Sequences and Mappings
- Class Patterns
- Guards and Wildcards