How to find whether a number belongs to a particular range in Python?

user46646 picture user46646 · Mar 6, 2009 · Viewed 114.3k times · Source

Suppose I want to check if x belongs to range 0 to 0.5. How can I do it?

Answer

unbeknown picture unbeknown · Mar 6, 2009

No, you can't do that. range() expects integer arguments. If you want to know if x is inside this range try some form of this:

print 0.0 <= x <= 0.5

Be careful with your upper limit. If you use range() it is excluded (range(0, 5) does not include 5!)