[Tutor] range/for list change behavior

Don Parris parrisdc at gmail.com
Tue Sep 30 04:33:56 CEST 2008


Hi all,

After a rather long (and unfortunate) break from tinkering with Python, I am
back at it.  I am working through the book Learning Python (based on 2.2/2.3
- I use 2.5), and in the chapter on while/for loops, ran across the
following example:

>>> L = [1, 2, 3, 4, 5]
>>> for i in range(len(L)):
...     L[1] += 1                         # this is a typo I made - should
have been L[i], not L[1].
...
>>> L
[1, 7, 3, 4, 5]

I did correct my typo, but what I do not understand is how range arrived at
a '7', where the '2' should be.  My best guess is that L[1] is treated as
the index of the value '2'.  I hope that learning how my error affected the
result will help me grasp the concept a little better.

Thanks!
Don
-- 
D.C. Parris
Minister, Journalist, Free Software Advocate
https://www.xing.com/profile/Don_Parris
http://www.linkedin.com/in/dcparris
sip:dcparris at ekiga.net <sip%3Adcparris at ekiga.net>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080929/e8e7c33b/attachment.htm>


More information about the Tutor mailing list