python for loop

woooee at woooee at
Wed Apr 1 04:30:15 CEST 2009

Counting from zero through n-1 is used because it is the memory offset
and not any kind of counter.  Simplified, if you are iterating through
a list, using a for loop or anything else, the first element/number is
at memory offset zero because it is at the beginning.  And if this is
a list of 4 byte integers, the offset for the second element is 1*4
bytes, etc.  This idea, that somehow the first element of a list is
the zero element is a fairly recent abnormality AFAIK.  It perhaps
comes from assumptions by people who are not familiar with what
happens inside of a programming language, assuming incorrectly, that
the (programming) world was created in their own image, and so
programming languages were generated in the way that they think they
were.  This is a bad assumption for any programmer.  Instead one
should get in the habit of saying, in general as well as in this
specific case, "This doesn't make sense.  I wonder how __I__ screwed
this up."  Hopefully this will be helpful advice.  Taking the attitude
that you have screwed up yet again will get to the heart of the
problem, and save many hours of frustration wondering why "this
language/computer doesn't do what it is supposed to do".

More information about the Python-list mailing list