Python TutorialLesson 19Free preview
Python Match
This lesson is part of Python · All levels
Summary
Since Python 3.10, the match statement provides a way to handle many cases in a concise, similar switch-case manner in other languages. Each case matches the values against the pattern, and the case _ acts as the default branch when no pattern matches. Match supports literal patterns, variable capture patterns, to guardian patterns with if in case.
Key points
- Matches are evaluated against a single value and case checks are done in sequence.
- Case _ is a wildcard that captures all the unsuitable conditions.
- A literal pattern compares fixed values such as numbers or text.
- The match requires Python version 3.10 above.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.