Match Statement in Python

Level 3126 keystrokes

Defines a function that uses match to classify a value as zero, an int, or other.

Python code

1def kind(x):
# match tries each case pattern from top to bottom
2 match x:
3 case 0:
4 return "zero"
# int() matches any other integer, so 7 lands here
5 case int():
6 return "int"
# _ is the wildcard that matches anything else, such as the string a
7 case _:
8 return "other"
9print(kind(0), kind(7), kind("a"))

Output

zero int other
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Python code and how often they appear.

  • "× 8
  • (× 6
  • )× 6
  • :× 5
  • ,× 2
  • _× 1

More Python examples at level 3

← Python typing practice