I'm really stuck in evaluating Boolean expressions. See the code:
def f(A):
if A=='a' or A=='b' or A=='c' ...:
return True
return False
Is there any convenient and elegant way to do this when A can equal to even more strings?
You can do
if A in ["a", "b", "c"]:
# do the thing
Since you are just returning the truth value, you can do
def f(A):
return A in ["a", "b", "c"]
The in
operator returns a boolean.
Mad Physicist gave the usual way it's done.
Here's another:
if any(A == thing for thing in ('a', 'b', 'c')):
# do stuff
This way isn't recommended for your specific example. It's more complicated. But sometimes, for more complicated situations, something similar to the above is useful.