python for loop

Grant Edwards grante at visi.com
Tue Mar 31 20:44:45 EDT 2009


On 2009-04-01, Lada Kugis <lada.kugis at gmail.com> wrote:

> in python for example:
> for i in range(1,n)
> goes from 1,2,3,4,...,n-1
> (that is, it goes from 1 up to, but not including n)
>
> Why is that so?

I'm surprised this isn't in the FAQ yet, but it doesn't seem to
be there.

> What were the reasons for that "not including" part?

So that range(n,m) behaves the same as slicing (e.g. similar to
someSequence[n:m])

Why does slicing behave that way?

So that someSequence[n:m]+someSequence[m:o] == someSequnece[n:o]

> It troubles me greatly,

Sorry about that.

> and I cannot see it's advantages over the "standard" "up to
> and including" n.

-- 
Grant




More information about the Python-list mailing list