From ffcc45ad986b70a5b1b39cc515bb7e7230db3c4d Mon Sep 17 00:00:00 2001 From: SAM <60264918+SAM-DEV007@users.noreply.github.com> Date: Tue, 4 Jun 2024 18:28:39 +0530 Subject: [PATCH] Update match_case_statement.md Added nested patterns --- .../advanced-python/match_case_statement.md | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/contrib/advanced-python/match_case_statement.md b/contrib/advanced-python/match_case_statement.md index 6941d98..22eeef9 100644 --- a/contrib/advanced-python/match_case_statement.md +++ b/contrib/advanced-python/match_case_statement.md @@ -162,6 +162,33 @@ Invalid ``` ### Nested Patterns +The patterns can be nested via various means. It can include the mix of the patterns mentioned earlier or can be symmetrical across. A basic of the nested pattern of a list with Patterns with a Literal and Variable is taken. Classes and Iterables can laso be included. +```python +def get_points(points: list) -> None: + match points: + case []: + print('Empty') + case [x]: + print(f'One point {x}') + case [x, y]: + print(f'Two points {x} and {y}') + case _: + print('More than two points') +``` +O/P: +``` +>>> get_points([]) +Empty + +>>> get_points([1]) +One point 1 + +>>> get_points([1, 2]) +Two points 1 and 2 + +>>> get_points([1, 2, 3]) +More than two points +``` ### Complex Patterns #### Wildcard