You can use guards:
match a:
case _ if a < 42:
print('Less')
case _ if a == 42:
print('The answer')
case _ if a > 42:
print('Greater')
Another option, without guards, using pure pattern matching:
match [a < 42, a == 42]:
case [True, False]:
print('Less')
case [_, True]:
print('The answer')
case [False, False]:
print('Greater')
Answer from Ajax1234 on Stack OverflowYou can use guards:
match a:
case _ if a < 42:
print('Less')
case _ if a == 42:
print('The answer')
case _ if a > 42:
print('Greater')
Another option, without guards, using pure pattern matching:
match [a < 42, a == 42]:
case [True, False]:
print('Less')
case [_, True]:
print('The answer')
case [False, False]:
print('Greater')
In this example, it's simpler to use good ol' if-elif, even if it means repeating the variable name.
if a < 42:
print('Less')
elif a == 42:
print('The answer')
elif a > 42:
print('Greater')
P.S. Using an enum and comparison function like in Thomas Sollie's answer is good for adding more structure to your program, but it seems like overkill for basic scripts and such.
Speaking of code style, avoid magic numbers: If all three 42's in your code represent the same thing, give it a name, for example:
the_answer = 42
if a < the_answer:
...
I can't say I fully understand the match/case statement yet, but I know a part of it involves more than just simple pattern matching. I know that the expression in the match statement, for example, actually runs and creates a result, such as an object if it's a call to a class.
So I'm wondering, if I don't want to do all that and just want to use it as a cleaner version of if/else, is this considered Pythonic, or is it overkill?
For example:
if some_value == True and some_other == 1:
do something
elif some_value == False and some_other == 1:
do something else
etc etc etcor:
match (some_value, some_other):
case (True, 1):
do something
case (False, 1):
do something else
etc etc etcFirst off, I *think* I'm getting the syntax correct! Second, I'm not actually creating any values with the expressions, so it feels like a glorified if/else construct, just cleaner looking.
Is this a valid use of match/case?
Thanks!
Videos
So odd - Claude.ai is generating a simple switch-case statement in python with conditional checks 3.10+ that is not working in Google Collab laboratoryAlso when searching for more info on peps.python.org the description of guards in switch-case stmt is not easy to comprehend. The simple question I’m looking for is can I have logical conditions in switch-case stmt in Python v 3.10+. Thanks
From claude.ai
This is not correct - Python does support logical conditions in match-case statements.
For example:
python
Copy code
x = 5
match x:
case x > 0 and x > 10:
print("x is greater than 10")
case x > 0 and x < 10:
print("x is between 0 and 10")
cases accept a "guard" clause, which you can use for this purpose:
match x:
case w if w in a:
return "132"
case w if w in b:
return "564"
case w if w in c:
return "798"
the w here actually captures the value of x, part of the syntax here too, but it's more useful in some other fancy cases listed on the linked whatsnew page.
In this example, it's simpler to use good ol' if-elif.
if x in a:
return "132"
elif x in b:
return "564"
elif x in c:
return "798"
If you don't want to repeat the variable name, you can use a loop:
for lst, retval in [
(a, "132"),
(b, "564"),
(c, "798"),
]:
if x in lst:
return retval
Note: This is very similar to my answer on "Is there a way to match inequalities in Python ≥ 3.10?".
Now, it would be different if you were doing any actual structural pattern matching, which is what match-case is intended for. Then you might want to use a guard clause like in tevemadar's answer. Say for example x is a structure:
match x:
case (w,) if w in a:
return "132"
case (_, w) if w in b:
return "564"
case (_, _, w) if w in c:
return "798"
You can do this without match-case, but it's not as elegant:
for n, lst, retval in [
(1, a, "132"),
(2, b, "564"),
(3, c, "798"),
]:
if len(x) == n and x[-1] in lst:
return retval
I read an article in Analytics Vidhya about match case statements and how much more efficient they are than traditional if/else statements.
if/else:
'''if grade == "A":print("Excellent!")elif grade == "B":print("Good!")elif grade == "C":print("Average!")else:print("Invalid grade!")'''
match/case:
'''match grade:
case "A":
print("Excellent!")
case "B":
print("Good!")
case "C":
print("Average!")
case _:
print("Invalid grade!")'''
I don't see the efficiency here. Is match/case actually useful?