[Tutor] FOR loops

William Park parkw@better.net
Sun, 3 Oct 1999 16:10:14 -0400


On Sat, Oct 02, 1999 at 10:07:04AM -0500, R Parker wrote:
> Hello, I am a beginner at python and have been learning it from the
> O'reilly book 'Learning Python'
> it has been pretty easy to understand for someone who has never
> programmed before, but it stumps me sometimes.......Thats where this
> message comes in, I got to the part that introduces FOR loops
> the book doesn't explain the concept very well and I don't understand
> how to use FOR loops in my programs.Could anyone explain what FOR loops
> do and how to implement them in my programming?? I would really
> appreciate it.

FOR-loop goes through each item of a sequence (list, tuple, or string)
starting from index=0 until the sequence item is exhausted.  For
example,

    for i in ["a", "b", "c"]:
	print i, i+i

will print 

    a aa
    b bb
    c cc

In the first loop, "a" is assigned to the index variable 'i'; in the
second loop, "b" is assigned to 'i'; and, in the third (and last) loop,
"c" is assigned to 'i'.  For list or tuple, the sequence items are
simply the items of list or tuple themselves; but, for string, the
sequence items are the characters of the string.


	Yours truly,
	William Park