In fact, your last interpretation is correct. A common idiomatic expression in programming is to test whether a number belongs to an interval, i.e. whether it is greater (or equal to) a
and less (or equal to) than b
. In C for example, we would do so:
if ( a < x && x < b) {
While in math we simply write:
a < x < b
Python decided to support this second form, interpreting something written as:
if a < x < b:
Like:
if a < x and x < b:
For this reason, three or more comparison operators in sequence are even interpreted as pairs united with "E". So it’s equivalent to write:
if 3 == 3 > 2:
And:
if 3 == 3 and 3 > 2:
Resulting, as you noted, in True
.
Note: equivalent as long as the operands have no side effects; a() == b() > c()
evaluate the functions only once, while a() == b() and b() > c()
appraisal b
twice.
Ah! If I had bumped into an example like
a < x < b
, perhaps doubt did not appear. With the==
gets a little darker.– bfavaretto