graywolf
graywolf

Reputation: 7490

Testing equality of three values

Are these equal?

xs[i] == xs[i+1] == xs[i+2]
xs[i] == xs[i+1] and xs[i+1] == xs[i+2]

Upvotes: 80

Views: 79287

Answers (7)

iabdalkader
iabdalkader

Reputation: 17312

Yes, however, when the comparisons are chained the common expression is evaluated once, when using and it's evaluated twice. In both cases the second comparison is not evaluated if the first one is false, example from the docs:

Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).

Upvotes: 18

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798676

Python chains such relational operators naturally (including in and is).

a() == b() == c() is functionally equivalent to a() == b() and b() == c() whenever consecutive calls to b return the same value and have the same aggregate side effects as a single call to b. For instance, there is no difference between the two expressions whenever b is a pure function with no side-effects.

The easiest way to show the slight difference:

>>> print(1) == print(2) == print(3)
1
2
3
True
>>> print(1) == print(2) and print(2) == print(3)
1
2
2
3
True

print() always returns None, so all we are doing is comparing Nones here, so the result is always True, but note that in the second case, print(2) is called twice, so we get two 2s in the output, while in the first case, the result is used for both comparisons, so it is only executed once.

Upvotes: 87

Pikesh Prasoon
Pikesh Prasoon

Reputation: 387

You can use set collection to test the equality

>>> a, b, c = 2, 2, 2
>>> len({a, b, c}) == 1
True
>>> a, b, c = 2, 2, 3
>>> len({a, b, c}) == 1
False

Upvotes: 3

Q. Qiao
Q. Qiao

Reputation: 827

It seems that you can also chain == with !=:

>>> a = b = c = 1
>>> a == b == c
True
>>> a == b == c == 1
True
>>> a == b == c == 2
False
>>> a == b == c != 2
True
>>> a == b == c != 1
False
>>> 

Upvotes: 1

Aaron Dunigan AtLee
Aaron Dunigan AtLee

Reputation: 2072

As answered by others, the answer is yes. However: beware of adding parentheses. For example:

>>> 1 == 2 == 0
False
>>> (1 == 2) == 0
True

In the second case, (1 == 2) evaluates to False, and then False == 0 evaluates to True, because Python allows comparison of booleans to integers.

Likewise:

>>> 0 == 0 == 1
False
>>> (0 == 0) == 1
True

Upvotes: 18

VB9-UANIC
VB9-UANIC

Reputation: 330

Yep, at the python's internals the comparison operators returns nor true neither false, they instead return the 'comparison result' object (cannot remember the class name, it was quite in past), and this object provides the _lt_, _gt_, _eq_ etc etc methods and become 'responsible' for the final result (and the 'comparison result' is casting to True or False at end of statement). That's a magic of semantic control python provides to you :)

Upvotes: 2

Joran Beasley
Joran Beasley

Reputation: 113978

yes you are correct ....

you can also do

5 > x > 1

or

1 < x < 5

Upvotes: 7

Related Questions