match Statement
match compares a value with patterns, like a powerful switch. Use _ as the default and | for alternatives.
Example
def kind(x):
match x:
case 0:
return "zero"
case 1 | 2:
return "small"
case [a, b]:
return f"pair {a},{b}"
case _:
return "other"
print(kind(0), kind(2), kind([7, 8]), kind("z"))Result
zero small pair 7,8 other
Exercise
Default case.
case :
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.