diff --git a/contrib/advanced-python/match_case_statement.md b/contrib/advanced-python/match_case_statement.md index e69de29..d4ad861 100644 --- a/contrib/advanced-python/match_case_statement.md +++ b/contrib/advanced-python/match_case_statement.md @@ -0,0 +1,22 @@ +# Match Case Statements +## Introduction +Match and case statements are introduced in Python 3.10 for structural pattern matching of patterns with associated actions. It offers more readible and +cleaniness to the code as opposed to the traditional `if-else` statements. They also have destructuring, pattern matching and checks for specific properties in +addition to the traditional `switch-case` statements in other languages, which makes them more versatile. + +## Syntax +``` +match : + case : + + case : + + case _: + +``` +A match statement takes a statement which compares it to the various cases and their patterns. If any of the pattern is matched successively, the task is performed accordingly. If an exact match is not confirmed, the last case, a wildcard `_`, if provided, will be used as the matching case. + +## Pattern Matching +As discussed earlier, match case statements use pattern matching where the patterns consist of sequences, mappings, primitive data types as well as class instances. The structural pattern matching uses declarative approach and it nexplicitly states the conditions for the patterns to match with the data. + +### Patterns with a Literal