Sub topic 2
Matching literal values
This chapter assumes you have Python 3.10 or later and know how to write a match statement with a few case blocks. It covers the simplest kind of pattern: the literal pattern.
What a literal pattern is
A literal pattern is a fixed value written directly after case. Python compares the subject (the value after match) against it. If they are equal, the case matches and its block runs.
def http_status(code):
match code:
case 200:
return "OK"
case 301 | 302:
return "Redirect"
case 404:
return "Not Found"
case _:
return "Unknown"
print(http_status(302)) # Redirect
print(http_status(500)) # UnknownThe | accepts alternatives, and _ is the wildcard default. Later chapters cover both in detail. Here we focus on which values you can write and how they are compared.
Which literals are allowed
You can use these forms:
- Integers, floats, and complex numbers, including negative ones such as
-1or-2.5. - Strings and bytes, including adjacent strings that Python joins, such as
"a" "b". - The constants
None,True, andFalse.
You cannot use arbitrary expressions. case 1 + 1: is a syntax error, and so is an f-string. A complex literal like 1 + 2j is the one exception.
Two kinds of comparison
Numbers and strings are compared with ==. The three constants None, True, and False are compared with is. This difference matters, because in Python True == 1 and False == 0, and 0 == 0.0.
def describe(value):
match value:
case None:
return "nothing"
case True:
return "true"
case False:
return "false"
case 0:
return "zero"
case "yes":
return "affirmative"
case _:
return "other"
print(describe(None)) # nothing
print(describe(True)) # true
print(describe(0.0)) # zero
print(describe(1)) # other
print(describe("yes")) # affirmativedescribe(0.0) returns "zero" because 0.0 == 0. describe(1) returns "other" because case True tests with is, and 1 is True is false.
The diagram shows how Python chooses the test.
Order can change the result
Because False == 0, a numeric case 0 also matches False. If it comes first, it wins.
def check(value):
match value:
case 0:
return "zero"
case False:
return "false"
case _:
return "other"
print(check(False)) # zeroThe case False block can never run for False here, because case 0 is tried first and False == 0 is true. Put the True and False cases before numeric ones if you need to tell them apart.
Strings are case sensitive
A string pattern matches only an exact string. case "quit" does not match "Quit". Normalize the subject first when you want a looser match.
def command(text):
match text.strip().lower():
case "quit" | "exit":
return "leaving"
case "help":
return "showing help"
case _:
return "unknown"
print(command(" Quit ")) # leavingCommon errors
- Writing a variable name where you meant a value. A bare name is not a literal. It captures the subject, so it matches everything. A later chapter explains this.
- Using an expression such as
case 60 * 60:. Compute the value in advance and match it through a dotted name, which a later chapter covers. - Forgetting the default. Without
case _, an unmatched subject silently does nothing, and thematchstatement produces no value.
Exercise
What does describe(1) return in the second code example, and why does it not return "true"?
Show answer
It returns "other". The pattern case True is tested with is, and the integer 1 is not the object True, even though 1 == True. No other case matches 1, so the wildcard runs.
Exercise
In the check function, how would you change the order of the cases so that check(False) returns "false"?
Show answer
Put case False: before case 0:. Cases are tried from top to bottom, and False == 0 is true, so case 0 would otherwise match first.