[Tutor] trouble understanding "for" loops

zehoefler at adelphia.net zehoefler at adelphia.net
Sat Nov 15 00:28:36 EST 2003

I was wondering if somebody could explain the "for" loop to me. I have a book on Python (Python Programming for the absolute beginner, ISBN 1-59200-073-8) and I couldn't understand the explanation very well. Here is an example:

for i in range(10):
    print i,

would make "0 1 2 3 4 5 6 7 8 9" be printed
I'm mostly confused as to the purpose of the variable and how it is used. I understand that something is read, and that it is assigned to the variable "i". What exactly is happening here? I noticed that it cycles through range... going one number at a time... I mostly get it, but I'm still having just a tad bit of trouble. Something like "for letter in word:" is printing each letter of the string "word" on each line (apparently it inserts line breaks [\n] itself)

P.S. Is there a way to get the Windows version of IDLE to, when you save your script, put the extension '.py' after the strings without having to enter it each time. I add the .py so I can access it faster, and its a bit tedious.

More information about the Tutor mailing list