Skip to content

[python/en] Match/Case Statement #5314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions python.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,22 @@ elif some_var < 10: # This elif clause is optional.
else: # This is optional too.
print("some_var is indeed 10.")

# Match/Case — Introduced in Python 3.10
# It compares a value against multiple patterns and executes the matching case block.

command = "run"

match command:
case "run":
print("The robot started to run 🏃‍♂️")
case "speak" | "say_hi": # multiple options (OR pattern)
print("The robot said hi 🗣️")
case code if command.isdigit(): # conditional
print(f"The robot execute code: {code}")
case _: # _ is a wildcard that never fails (like default/else)
print("Invalid command ❌")

# Output: "the robot started to run 🏃‍♂️"

"""
For loops iterate over lists
Expand Down