[Tutor] A range of numbers
Don Arnold
darnold02 at sprynet.com
Mon Dec 1 19:14:06 EST 2003
On Dec 1, 2003, at 5:53 PM, Gregor Lingl wrote:
> Hi Leung!
>
> There is a range function in Python:
>
>>>> range(5)
> [0, 1, 2, 3, 4]
>>>> range(5,10)
> [5, 6, 7, 8, 9]
>
> It outputs lists of integer values.
> play around with it to learn how it works.
> (Even to call range(10,20,2), i. e. with
> 3 arguments, is possible)
>
> On the other han you can check if some value
> is in a certain range with the in - Operator:
>
>>>> y = 4
>>>> y in range(5)
> True # 1 in older Versions of Python
>>>> y in range(5,10)
> False # or 0
>
> so if you first extract the coordinates of
> your mouse-position:
>
>>>> x,y = mouse_position()
>
> then you can easily check if y is in the desired
> range.
>
> HTH
> Gregor
>
I never thought of doing range checking like that before. Cool. But I
still prefer:
x, y = mouse_position()
if 100 <= x <= 200 and 100 <= y <= 200:
print "You clicked!"
That way, you don't generate a throwaway list, and you don't have to
worry
about the potential off-by-one error that can happen when using range().
Just my 2 cents,
Don
More information about the Tutor
mailing list