←Previous page

04CONDITIONALS · MATCH

Pick a case with match.

Many languages have a switch to pick one option from a list. Python calls it match, and each option is a case.

IN REAL LIFE

One box, many options.

Look at one thing, like the day or the button, and pick the option that fits.

What day is it?

Monday: swimming. Wednesday: art class. Saturday: the park. Any other day: play at home.

Roll the die

1: hide and seek. 2: jump rope. 3: draw a monster. Any other number: you choose!

The juice machine

Press A: orange juice. Press B: mango juice. Any other button: water.

LOOK FIRST

switch in other languages, match in Python.

match looks at a box

match command: means: let us look at what is inside command.

case is one option

case "jump": runs its path when the box holds "jump".

case _ catches the rest

The _ means “anything else”. It works like else.

switch → match Heads up: Python has no switch word. Since Python 3.10, it uses match and case.

STEP BY STEP

Find the case that fits.

PyBot compares the box with each case, from the top.

day = "saturday"A box keeps the word saturday.
match day:Look inside the day box.
case "friday":Is it friday? No. Try the next case.
print("school")Skipped.
case "saturday":Is it saturday? Yes!
print("play")This path runs.
case _:Skipped. A case already fit.
print("rest")case _ runs only when no other case fits.

LOOK FIRST

Three rules for match.

Only one case runs

Python picks the first case that fits, then leaves the match.

case _ goes last

_ fits anything, so the cases below it would never get a turn.

| means or

case "saturday" | "sunday": fits both days with one path.

RUN IT

Send PyBot a command.

Guess first. Then run real Python and check.

1

Guess the result

command = "jump" match command: case "walk": print("PyBot walks") case "jump": print("PyBot jumps") case _: print("PyBot waits")

What will Python show?

command.pyREAL PYTHON
OUTPUT
Ready. Press Run Python.
Python runs here thanks to Pyodide ♥

Pyodide 314.0.3 runs CPython in your browser with WebAssembly.

Open-source under the Mozilla Public License 2.0. License

Now change one thing: Change "jump" to "walk", then to "dance". Which case fits each time?

YOUR TURN

Find the case.

Try each tiny question. A wrong answer becomes something to review.

0Completed
10Not tried
0Review
01

Today's activity

Monday: swimming. Wednesday: art class. Saturday: the park. Any other day: play at home. Today is Friday.

What does PyBot do today?

02

Python's switch

Other languages say switch. Which word does Python use?

03

Pick the fruit

fruit = "apple" match fruit: case "banana": print("yellow") case "apple": print("red") case _: print("unknown")

What does Python show?

04

No case fits

animal = "cow" match animal: case "dog": print("woof") case "cat": print("meow") case _: print("hmm")

What does Python show?

05

The underscore

What does case _ mean?

06

Two days, one path

day = "sunday" match day: case "saturday" | "sunday": print("weekend") case _: print("school day")

What does Python show?

07

How many cases?

color = "red" match color: case "red": print("stop") case _: print("other")

_ fits anything, even red. What shows?

08

Same job

match can do the same job as…

09

Fix PyBot's code

PyBot used the word from other languages. Change it to Python's word.

Goal output
meow
OUTPUT

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

match picks one case for the value in a box.

Other languages call it switch. In Python it is match, with case _ for anything else.