CHAPTER 21 •
PYTHON
Python Match
Learn Python's match-case statement for pattern matching.
01 What is this?
Learn Python's match-case statement for pattern matching.
02 Syntax
Python Syntax
match value:
case pattern:
statement
03 Example
Python Example
day = 2
match day:
case 1:
print('Monday')
case 2:
print('Tuesday')
case 3:
print('Wednesday')
case _:
print('Other day')
04 Important Points
match-case is available in modern Python versions.
05 Common Mistakes
Use case _ as a default pattern when needed.
06 Real World Use
Pattern matching can simplify menu and command-based programs.
07 Practice This Topic
Now try the example yourself in the AIFA Python Editor.
- Read the example carefully.
- Change the values.
- Run the code.
- Observe the output.
- Try creating your own example.