python for loop

Lada Kugis lada.kugis at
Wed Apr 1 03:57:25 CEST 2009

On Tue, 31 Mar 2009 18:05:19 -0700, Gary Herron
<gherron at> wrote:

>This debate has been around for decades, in both mathematics and 
>Should a loop through n things use indices 
>    1, 2, ..., n
>    0, 1, ..., n-1 ?
>Fortran tends to go with the former (1-based indices) , while modern 
>languages usually go with the latter (0-based indices).   (And just for 
>the record, your range(1,n) seems to be trying to coerce Python from 
>0-based to 1-based.)

Oh, don't mind those. I just bumped them off the top of my head for
purpose of comparison.

>The arguments for each are many, but are often centered around the 
>prevalence of  the proverbial off-by-one error.    Here's a nice 
>explanation of the off-by-one error:
>Google can provide many more.
>My personal view is that 0-based loops/indices is *far* more natural, 
>and the 1-based loops/indices are a *huge* source of off-by-one errors.  
>But I'm sure others will argue over that point.

Yes, that's it. I won't argue over it, since I can't change it, but 1
is still more natural to me (it is "the real world" way). The above
pros seem to me to be very little compared to the cons of the ... and
the (m-n) point Chris was trying to explain doesn't seem that relevant
to me.
I always thought since python is trying so hard to be as intuitive as
it can get it would be the other way (you have 1 apple, you start
counting from 1 ... 

but then again, I have training of an engineer, not as an CS, so we
probably have a completely different view of looking at things.

Thank you for the explanation Gary.


>Gary Herron

More information about the Python-list mailing list