How should I code to test for an integer value of 0 or 1
The right answer is to use OR-Patterns, described in PEP 622:
Multiple alternative patterns can be combined into one using |. This
means the whole pattern matches if at least one alternative matches.
Alternatives are tried from left to right and have a short-circuit
property, subsequent patterns are not tried if one matched.
x = 1
match x:
case int(0 | 1):
print('case of x = int(0) or x = int(1)')
case _:
print('x != 1 or 0')
Output: 'case of x = int(0) or x = int(1)'
Type insensitive would be like this:
x = 1.0
match x:
case 0 | 1:
print('case of x = 0 or x = 1')
case _:
print('x != 1 or 0')
Output: 'case of x = 0 or x = 1'
In case you want to check for each case separately, you would do:
x = 1.0
match x:
case int(0):
print('x = 0')
case int(1):
print('x = 1')
case _:
print('x != 1 or 0')
Output: x != 1 or 0
x = 1.0
match x:
case 0:
print('x = 0')
case 1:
print('x = 1')
case _:
print('x != 1 or 0')
Output: x = 1