mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-06 06:47:54 +02:00
[python/en] Match/Case Statement (#5314)
* Update python.md Include Match/Case Statement * Update python.md include OR Pattern and Conditionals
This commit is contained in:
committed by
GitHub
parent
7a104b4025
commit
155fc5db37
16
python.md
16
python.md
@@ -404,6 +404,22 @@ elif some_var < 10: # This elif clause is optional.
|
|||||||
else: # This is optional too.
|
else: # This is optional too.
|
||||||
print("some_var is indeed 10.")
|
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
|
For loops iterate over lists
|
||||||
|
Reference in New Issue
Block a user