python for loop

Lada Kugis lada.kugis at gmail.com
Wed Apr 1 02:48:15 CEST 2009


On Wed, 01 Apr 2009 11:42:20 +1100, Ben Finney
<ben+python at benfinney.id.au> wrote:

>Lada Kugis <lada.kugis at gmail.com> writes:
>
>> 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)
>
>Also, ?range(n)? counts from 0 to n-1.
>
>> Why is that so ?
>
>The answer is in the documentation for ?range?:
>
>    For example, range(4) returns [0, 1, 2, 3]. The end point is
>    omitted! These are exactly the valid indices for a list of 4
>    elements.

Yes, but why didn't they start indexing from 1 then, like fortran for
example ?
It would solve for [1,2,3,4] length of list (just returns the last
indice, in this case 4), "up to and including" problem, ...

Lada

>
>You may be interested in the iterator-generating functions in 
>?itertools? <URL:http://docs.python.org/library/itertools>.



More information about the Python-list mailing list