Match - Case Statement
• Only available from Python 3.10 version.
• Used to execute statements based on specific pattern values. Often used in place of an if-elif-else
statement when there are large number of conditions, especially patterns to match.
• Pattern value can be a number, string, an expression, iterable, class instance, etc.
• Subject value can be any data type
• a _: is used to accept any value not matched with list of values
match subject:
case <pattern1>:
action 1
case <pattern2>:
action 2
case _:
action for a no match
• One can combine several literals in a single pattern using |
• Case <pattern1> | <pattern2> | <pattern3>:
action