Question
If we have a transitive relation of expressions combined with and
, like (a < b) and (b < c) and (c < d)
, can this be shortened?
Answer
Yes, this can be done by chaining
the expressions with a transitive relation. Because of the order of operations, each and
operator has the same precedence which means that they will just evaluate from left to right as they appear in the entire expression.
Example
a = 1
b = 2
c = 3
d = 4
# Original expression
(a < b) and (b < c) and (c < d) # True
# Using chaining, we can shorten this.
# This will evaluate to the same result as above
a < b < c < d # True