When comparing one value with multiple values, can we just separate each value with or?
Answer
No, when comparing one value with many other values, or cannot be used to check, for instance, if “a value equals some value or another value or another value.” Each side of the or must be a valid expression on their own, so this would not work.
Example
# This would not compare value with 10, 11, 12.
if (value == 10 or 11 or 12):
print("Fourth quarter")
# Instead, each comparison must be separate and
# valid on their own on each side of each "or".
if (value == 10 or value == 11 or value == 12):
print("Fourth quarter")
or ( as well as and) does not necessarily return True or False.
or goes through its operands left to right, and returns the value of first one that has a “truth value” of True. If none of them have a truth value of Truth value of True, it returns the value of the last operand.
If all of the operands are True or False, or expressions that resolve to True or False, it will work as you expect.
But if the operands are other objects (ints in the example), Python goes by the rules:
The truth value of False, 0, any empty container or any expression that evaluates to one of those objects is False.
The truth value of everything else is True.
x = 1 or 2
y = 0 or []
z = 3 > 2 or 5 < 10
print(x)
print(y)
print(z)
# Output
1 # first True
[] # last value, as both have a truth value of False
True # Expression evaluates to true
(and works in a similar way: it returns the value of the first object that has a truth value of False; otherwise, it returns the value of the last operand tested.)
value = 10
if (value == 10 or 11 or 12):
print("Fourth quarter")
value = 3.14
if (value == 10 or 11 or 12):
print("Fourth quarter")
# Output:
Fourth quarter
Fourth quarter
The expression (10 or 11 or 12) will always return 10, according to what I wrote above. Since 10 has a truth value of True, the if clause will always pass, no matter the value of the variablevalue.
Is the precedence of the comparison operator(==) lesser than ‘or’ operator? As you have said that the logical result is prioritized more than the result of comparison. Does python have a precedence following nature?
== is of higher precedence than and, and and is higher than or, according to the docs.
In the above examples, value == 10 is evaluated first, returning True in one case, and False in the next.
That leaves:
True or 11 or 12 # case 1 False or 11 or 12 # case 2
Let’s evaluate those:
print(True or 11 or 12)
print(False or 11 or 12)
# Output:
True # First True
11 # First True
Both of these have a truth value of True, so both if blocks are triggered.
Python definitely has an order of operator precedence, which I linked to above. Note that in that table, precedence is from least to greatest (i.e., least on top). Expressions containing operators of the same precedence are evaluated left to right.
x = 1 or 2
y = 0 or []
z = 3 > 2 or 5 < 10
print(x)
print(y)
print(z)
# Output
1 # first True
[] # last value, as both have a truth value of False
True # Expression evaluates to true
Remember that Python prints, in the case of or, the first value (object) that that has a boolean (“truthy”) value of True, and the second value if both have a boolean value of False. In this case, [] is the second value (object). (And, likewise, and returns first False or second True object.)
The point is that, unlike other comparison operators, which return either True or False or raise a TypeError if the values cannot be compared, and and or return the value of one of their operands. If those operands are boolean expressions that return True or False, the behavior is what you expect; if not, you will see one of the operands themselves (i.e., one of the objects) returned.
x = 'abc'
y = []
print(bool(x))
print(bool(y))
print(x or y)
print(x and y)
print(x >= y)
Output:
True
False
abc # first True
[] # first False
Traceback (most recent call last):
File "C:\Users\path\totest.py", line 9, in <module>
print(x >= y)
TypeError: '>=' not supported between instances of 'str' and 'list'
patrickd314 covers this in his answer? 11 and 12 are not compared to anything, as such, python will evaluate if they are truthy values. Truthy means a value which is considered true. Positive integers are considered true.
Hi, I know we’re discussing logical and relational operators so it makes sense to see to use OR but I agree with “tajaddini” that the best way to check if a value is 10 or 11 or 12 is to use IN rather than OR
e.g. if value in [10, 11, 12]: …
First thanks to all contributors to this interesting conversation. I find it really valuable. It’s clear that OR/AND kind of misbehave (or at least are mischievous!) when evaluating inputs that do not result in a clear (at least to me) logical output. Hence the usefulness of the IN function mentioned above. Clearly it’s helpful to understand the behaviors when debugging, but is it actual practice to deliberately create the type of outputs mentioned above eg value of 3.14 printing “fourth quarter”. Surely this complexity is never intended for deliberate use on coherent code? An example would be great to explain if otherwise