tags:

views:

921

answers:

7

Suppose i want to check 1.1 belongs to range 0 to 0.5 how can i do it?

with range i can do :

for i in range(0,0.5):
  if i == 1.1:
    print 'yes'
  if i != 1.1:
    print 'no'

Is ther any other way to do this ????

+4  A: 
print 'yes' if 0 < x < 0.5 else 'no'

range() is for generating arrays of consecutive integers

vartec
thank you..i got range() conceptis there any built-in method ?
built-in method for what?
SilentGhost
built-in method for writing 0 < x < 0.5?
vartec
how's writing 0<x<0.5 not built-in? why do you need method?
SilentGhost
i want to know if there is any method
Using built-in methods: (0.0).__lt__(x).__and__((5.0).__gt__(x))
unbeknown
ha-ha, that's hilarious.
SilentGhost
@vartec: Add `print` before 'yes' to match the question exactly.
J.F. Sebastian
@Sebastian: done, however, I'm not sure anymore what the question is exactly ;-)
vartec
def(upper, lower, between): print "yes" if lower < between < upper else "no"Build your own damn methods. What has the world come to when people expect something as simple as a 10 token function to be built in.
Josh Smeaton
+11  A: 

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!)

unbeknown
-1: No reference to the documentation and no example of what range really does. +1: Being polite in the face of an absurd question.
S.Lott
+2  A: 
>>> s = 1.1
>>> 0<= s <=0.2
False
>>> 0<= s <=1.2
True
SilentGhost
A: 
def belongsTo(value, rangeStart, rangeEnd):
        if value >= rangeStart and value <= rangeEnd:
                return True
        return False

Now you can:

print "1.1 belongs to <0, 0.5>:", belongsTo(1.1, 0, 0.5)
etam
that's brilliant, you don't think that any of the above proposed method is at least cleaner? or shorter? or more pythonic?
SilentGhost
how do I hate these if (true) return true else false.
vartec
@vartec: I feel your pain.
unbeknown
... what is *number* ? i think you should *value*, then the condition could be rangeStart <= value <= rangeEnd. You can return the condition directly. In fact it returns True if it's True, else False, you don't need *if* or double *return*
Andrea Ambu
I provided solution that author will understand. IMO "return left <= value <= right is harder to understand by beginners.
etam
@etam: "if x: return True" is usually more confusing than left <= value <= right.
S.Lott
A: 

To check whether some number n is in the inclusive range denoted by the two number a and b you do either

if   a <= n <= b:
    print "yes"
else:
    print "no"

use the replace '>=' and '<=' withe '>' and '<' to check whether n is in the exclusive range denoted by a and b (i.e. a and b are not themselves members of the range)

Alternatively, you can also check for

if (b - n) >= a :
    print "yes"
    ...

Range will produce an arithmetic progression defined by the two (or three) arguments converted to integers. See the documentation. This is not what you want I guess.

VoidPointer
your alternative version is just outright wrong
SilentGhost
wrong as in concept-wrong, not typo-wrong
SilentGhost
Well, it works for integers. For floats it *can* work, depending on values of b and n.
Abgan
+1  A: 

Old faithful:

if n >= a and n <= b:

And it doesn't look like Perl (joke)

Ali A
A: 

I would use the numpy library, which would allow you to do this for a list of numbers as well:

from numpy import array
a = array([1, 2, 3, 4, 5, 6,])
a[a < 2]
dalloliogm