Tanishq dubey Tanishq dubey - 9 months ago 48
Python Question

Return if a number is between two values [Python]

Quite a simple question, but I have no clue on how to implement this.

Essentially:

>>> r = range(4,-1)
>>> 3 in r
False
>>> q = range(-1,4)
>>> 3 in q
True


As you can see, I have the same bounds, -1 and 4, and the same test value, so how do I say that '3' is between'-1' and '4' when I do not know the order that they are given to me in?

Answer Source

Why not sort the bounds first?

r = range(*sorted((4, -1)))
q = range(*sorted((-1, 4)))