Which version of Python introduced the match-case statement?
a) Python 3.8
b) Python 3.9
c) Python 3.10
d) Python 3.7
Answer:
c) Python 3.10
Explanation:
The match-case
statement was introduced in Python 3.10 as part of the language’s support for structural pattern matching. This feature allows developers to write more expressive and readable code by matching patterns in data structures.
# Example of match-case in Python 3.10
command = "start"
match command:
case "start":
print("Starting...")
case "stop":
print("Stopping...")
case _:
print("Unknown command")
This example demonstrates how match-case
can be used to handle different commands or inputs, providing a clear and concise way to manage multiple possibilities.
Python 3.10’s introduction of match-case
has been a significant enhancement, particularly for developers working with complex data structures or protocols where pattern matching is essential.