["python"]
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]
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.