Skip to content

Commit 155fc5d

Browse files
authored
[python/en] Match/Case Statement (#5314)
* Update python.md Include Match/Case Statement * Update python.md include OR Pattern and Conditionals
1 parent 7a104b4 commit 155fc5d

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

python.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,22 @@ elif some_var < 10: # This elif clause is optional.
404404
else: # This is optional too.
405405
print("some_var is indeed 10.")
406406

407+
# Match/Case — Introduced in Python 3.10
408+
# It compares a value against multiple patterns and executes the matching case block.
409+
410+
command = "run"
411+
412+
match command:
413+
case "run":
414+
print("The robot started to run 🏃‍♂️")
415+
case "speak" | "say_hi": # multiple options (OR pattern)
416+
print("The robot said hi 🗣️")
417+
case code if command.isdigit(): # conditional
418+
print(f"The robot execute code: {code}")
419+
case _: # _ is a wildcard that never fails (like default/else)
420+
print("Invalid command ❌")
421+
422+
# Output: "the robot started to run 🏃‍♂️"
407423

408424
"""
409425
For loops iterate over lists

0 commit comments

Comments
 (0)