Tanishq dubey Tanishq dubey - 1 year ago 96
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.


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

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)))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download