python for loop

Carl Banks pavlovevidence at gmail.com
Wed Apr 1 03:40:17 EDT 2009


On Mar 31, 7:23 pm, Lada Kugis <lada.ku... at gmail.com> wrote:
> On 01 Apr 2009 01:26:41 GMT, Steven D'Aprano
>
> <ste... at REMOVE.THIS.cybersource.com.au> wrote:
>
> >Why Python (and other languages) count from zero instead of one, and
> >why half-open intervals are better than closed intervals:
>
> >http://www.johndcook.com/blog/2008/06/26/why-computer-scientists-coun...
> >http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html
>
> steven, thanks for answering,
>
> yes, i saw the second one a little time ago (someone else posted it as
> well in really cute handwriting version :) and the first time just
> now, but the examples which both of them give don't seem to me to be
> that relevant, e.g. the pros don't overcome the cons.
>
> imho, although both sides (mathematical vs engineer) adress some
> points, none of them give the final decisive argument.
> i understand the math. point of view, but from the practical side it
> is not good. it goes nicely into his tidy theory of everything, but
> practical and intuitive it is not. as i said, being an engineer, i
> tend towards the other side,


Lada,

I am also an engineer, and I can tell your idea of intuitive is not
universal, even among engineers.  I certainly do not lean toward one-
based indexing.

>From a programming standpoint--and remember Python is a programming
language--zero-based indexing eliminates the need for a whole lot of
extra +1s and -1s when indexing, slicing, and iterating, a lot more
than it causes, and that is worth the "cost".  This might not be
apparent to you if you never tried seriously taking advantage of
indexing from zero, or if your programming experience is very narrow.
These both seem to be true for you, so you'll just have to take my
word for it.

I'm sorry that the zero-based indexing isn't the most natural for your
field.  There are some things about Python that aren't the natural
choice for me, too.

I would, however, highly recommend that you avoid the ridiculous
pseudo-one-based indexing trick you pulled when interfacing C to
Fortran.  Python is zero-based, and you are going to have a much
better experience if you don't fight it.  I assure you that it is
really not that hard to cope with indices being off by one from what
you have written down.  Really.  I have to interface two languages,
one of which indexes from one, the other from zero, and it really is
no problem.


Carl Banks



More information about the Python-list mailing list