What Is match?
The match statement was introduced in Python 3.10.
It lets you compare a value against different patterns and run the code for the first matching case.
If you’ve used switch-case statements in other programming languages, match may feel familiar.
Here’s a simple example:
number = 4
match number:
case 0:
print("Number is zero")
case 4:
print("Number is four")
case 10:
print("Number is ten")
# Output:
# Number is fourPython checks the cases from top to bottom.
When it finds a match, it runs that block of code.
Basic Syntax
The basic structure looks like this:
match value:
case pattern1:
# Code to run if pattern1 matches
case pattern2:
# Code to run if pattern2 matches
case _:
# Code to run if nothing matchesThe match statement contains one or more case blocks.
Each case describes a pattern that Python checks against the value.
Using case _
What if none of the cases match?
You can use:
case _:The _ acts as a catch-all pattern.
For example:
number = 7
match number:
case 1:
print("One")
case 2:
print("Two")
case 3:
print("Three")
case _:
print("Something else")
# Output:
# Something elseSince 7 doesn’t match 1, 2, or 3, Python uses case _.
You can think of case _ as similar to the final else in an if-elif-else statement.
Matching Multiple Values
You can use the | symbol to make one case match more than one value.
For example:
day = "Saturday"
match day:
case "Saturday" | "Sunday":
print("It's the weekend!")
case _:
print("It's a weekday.")
# Output:
# It's the weekend!Here, both "Saturday" and "Sunday" trigger the same block.
The | works a little like or from the logical operators:
"Saturday" OR "Sunday"This is useful when multiple values should lead to the same result.
No Fallthrough
If you’ve used switch statements in some other languages, there’s another difference worth knowing.
Unlike switch in some other languages, Python’s match does not fall through to the next case.
Once Python finds a matching case, it runs that block and stops checking the remaining cases.
You don’t need a break statement.
For example:
number = 2
match number:
case 1:
print("One")
case 2:
print("Two")
case 3:
print("Three")
# Output:
# TwoOnly the matching case runs.
match With User Input
We can combine match with input().
For example:
choice = input("Choose a fruit: ")
match choice:
case "apple":
print("You chose an apple.")
case "banana":
print("You chose a banana.")
case "orange":
print("You chose an orange.")
case _:
print("Unknown choice.")If the user enters apple, Python prints You chose an apple.
If the user enters something else, the case _ block runs.
match vs if-elif-else
You can often use either match or if-elif-else depending on what you’re trying to do.
For example, this:
number = 2
if number == 1:
print("One")
elif number == 2:
print("Two")
elif number == 3:
print("Three")
else:
print("Something else")can be written using match:
number = 2
match number:
case 1:
print("One")
case 2:
print("Two")
case 3:
print("Three")
case _:
print("Something else")For several exact values, match can make the code easier to read.
Match With a Condition
match can also use a condition called a guard.
For example:
number = 4
match number:
case 0:
print("Zero")
case _ if number < 10:
print("Less than 10")
case _:
print("10 or greater")
# Output:
# Less than 10Here:
case _ if number < 10:means the catch-all pattern is only used when number < 10 is also True.
Important: Python Version
match and case were introduced in Python 3.10.
So if you’re using an older version of Python, this syntax won’t work.
You can check your Python version with:
python --versionor:
python3 --version👉 Next tutorial: Python Loops