Return if a number is between two values [Python]

Question:

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?

Asked By: Tanishq dubey

||

Answers:

range doesn’t do what you think it does.

It creates a list; it’s not a numeric range (edit: actually it does in Python 3).

Just imagine a case, when the lowerEnd is -20000 and the upperEnd is +20000. Using the range( -20000, 20000 ) and comparing num in such a range() is a waste of both memory and CPU-power.

It is quite enough to compare a num against lowerEnd & upperEnd limit

You want to check:

num = 3
print(-1 < num < 4)
Answered By: 101

Why not sort the bounds first?

r = range(*sorted((4, -1)))
q = range(*sorted((-1, 4)))
Answered By: John La Rooy

Why don’t you try printing each step of your code snippet ? The code becomes self-explanatory to an extent. range() returns a list actually. So when this line is executed

r = range(4,-1)
print r 

[]   ## returned an empty list so which is why 3 in r returns False.

But when you execute like this

q = range(-1,4)
print q

[-1, 0, 1, 2, 3]  ## Returned a list with 3 in it so 3 in q returns True

Now you want to check falls in range then you can do like this

if -1<= 3 <= 4:
   print "Falls in the range"
else:
   print "not in range"

output:

Falls in the range
Answered By: d-coder

Use a if statement to find the order

if c > d:
    c, d = d, c 

r = range(c, d)
Answered By: Ravdeep

Maybe this?

r = list(xrange(4,-1,-1))
3 in r

True

q = list(xrange(-1,4,1))
3 in q

True
Answered By: TinnyT

The syntax of the range builtin needs a little while to be grasped but when working with the interactive interpreter help(range) is always a helping hand…

Python 2.x

>>> print range(4,-1)
>>> print range(-1,4)

Pyton 3.x

>>> print(list(range(4,-1)))
>>> print(list(range(-1,4)))

but there is a problem (perhaps not with your specific issue) even if you add a step argument to the invocation of range (example for Python 2.x)

>>> print range(4, -1, -1)
>>> print range(-1,4)

as you can see, the two ranges are not the same: the start argument is in the list, the stop one is not.


You can think of it in these terms: “in a range, the stop argument means stop before“.


Answered By: gboffi

As answered by 101 and d-coder, your question to find a value between two other values should be constructed using < or <= operators, depending on what you want. Assuming that you know that r is a simple collection, like a list or tuple or range, but you don’t know it’s ordering, you could write:

>>> r = (-1,4,2,3,0)
>>> min(r) < 3 < max(r)
True
>>> min(r) < 2.5 < max(r)
True

The in operator on the other hand will test whether your value is one of the values in the list

>>> 3 in r
True
>>> 2.5 in r
False

Finally, your first example failed because you did not specify a negative step when trying to define ‘r’ as a decreasing sequence. Here’s an example where I’ll specify a step size of -2 as the 3rd argument of range():

>>> a = range(4,-1)
>>> print a
[]
>>> b = range(5,-1,-2)
>>> print b
[5, 3, 1]
>>> 3 in a
False
>>> 3 in b
True
Answered By: Bill Olsen
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.