Although abs(x - y)
or equivalently abs(y - x)
is preferred, if you are curious about a different answer, the following one-liners also work:
max(x - y, y - x)
-min(x - y, y - x)
max(x, y) - min(x, y)
(x - y) * math.copysign(1, x - y)
, or equivalently (d := x - y) * math.copysign(1, d)
in Python =3.8
functools.reduce(operator.sub, sorted([x, y], reverse=True))