Comparisons can be linked in a chain. The follow example tests three values. Both tests must be true for the comparison to be true.
>>> if 1 < 2 < 3: ... print "yes!"
TECHNICAL Linked comparisons can be hard to read. In some cases, combining STUFF comparisons with Boolean operators (and, or, not) and parentheses will improve readability of your code. Compare these pairs of expressions and decide which are easier to read:
The Boolean operators have lower priorities than comparison operators—that is, they are evaluated after comparison operators. You can use parentheses to make sure comparisons are evaluated in the order you want.
Was this article helpful?