Back
+10 XP
Concept

Modern Pattern Matching: `match / case`

Pattern matching cleanly checks a variable against multiple values instead of a long `if/elif` chain.

python
status = 404
match status:
    case 200:
        print("OK")
    case 404:
        print("Not Found")
    case _:
        print("Unknown")

# 55. Modern Pattern Matching: `match / case` Pattern matching cleanly checks a variable against multiple values instead of a long `if/elif` chain: ```python status = 404 match status: case 200: print("OK") case 404: print("Not Found") case _: print("Unknown") ``` The underscore `_` is the wildcard fallback—it catches any value that didn't match preceding cases.

Checkpoint

What does `case _:` do at the end of a `match` block?

Practice

Use `match / case` on `cmd = "STOP"` to print "Halted" when it matches "STOP".

Terminal
01
Tap code blocks below to assemble
Available Blocks