[Tutor] FOR loops

Evgeny Roubinchtein eroubinc@u.washington.edu
Sat, 2 Oct 1999 09:29:19 -0700 (PDT)


On Sat, 2 Oct 1999, R Parker wrote:

[...snip...]
>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
[...snip...]

I'll give this a shot, and if I mess up somewhere, other people will
(hopefully) correct me.
You typically use for-loops when you have a list (or a tuple) of things
and you want to do something to each element of the list.

For example:
>>> friends = ('Jack', 'Jill', 'John')
>>> for friend in friends:
...     print 'Hello,', friend
... 
Hello, Jack
Hello, Jill
Hello, John

I have a difficult time thinking of an example you must have for-loops or
else you cannot say what you wish to say.  I could have written the
example above with a while-loop, for instance like this:

>>> friends = ('Jack', 'Jill', 'John')
>>> i = 0
>>> while i < len(friends):
...     print 'Hello,', friends[i]
...     i = i + 1
... 
Hello, Jack
Hello, Jill
Hello, John

In the example above, I made a list explicitely, but it is probably more
common to let Python make the list for me.  For instance, if I have a
shopping list like this:

shopping_list = {'ham' : 2.25, 'eggs' : 1.50, 'milk' : 1.75}
,I could figure out the total like so:

>>> total = 0
>>> for thing in shopping_list.keys():
...     total = total + shopping_list[thing]
... 
>>> print total
5.5

Here I am letting Python make the list of items for me by saying 
shopping_list.keys() first, and then I get the price for each thing on my
list, and sum the prices up.  Here's what shopping_list.keys() does:

>>> shopping_list.keys()
['milk', 'eggs', 'ham']

Again, I don't _really_ need a for-loop here, I could have used a
while-loop, for example like so:

>>> i = 0 
>>> total = 0 
>>> k = shopping_list.keys()
>>> while i < len(k):
...     total = total + shopping_list[ k[i] ]
...     i = i + 1
... 
>>> print total
5.5

(but using for-loop is more convenient, since I have to say less to
achieve the same result).

It is very common to want to do something to a list of integers, and since
you probably don't want to write out a list of integers from 1 to 1000 by
hand every time you want to use one, Python provides a function called
range() that makes the list for you

Here is a way to do factorial of 5:
>>> fact = 1
>>> for i in range(2, 6):
...     fact = fact * i
... 
>>> print fact
120

You can see that you could do the same thing with a while-loop. (How?)

What follows probably will make more sense after you have read about
exceptions (I think they are in Ch 7).

Here's another way to re-write my first example with a while-loop, that
is, as I understand it, closer in spirit to what Python "actually does"

>>> friends = ('Jack', 'Jill', 'John')
>>> i = 0
>>> try:
...     while 1:
...             friend = friends[i]
...             print 'Hello,', friend
...             i = i + 1
... except IndexError:
...     pass
... 
Hello, Jack
Hello, Jill
Hello, John



--
Evgeny Roubinchtein, eroubinc@u.washington.edu
...................
Unprecedented performance:  Nothing ever ran this slow before.