SV: [Tutor] Why different behaviours?

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Fri, 26 Oct 2001 14:10:02 -0700 (PDT)


On Fri, 26 Oct 2001, Danny Kohn wrote:

> Danny, the problem is not the print formatting in gerenal although it
> might have been. What bugs me is why the print statement in the first
> example is not printing out one character per line (as there is no
> comma last!). It seems to me that there is something more fundamental
> I have not grasped yet. Why is there a difference between when a
> string is printed and when it is used in a for loop as index?

The difference is that Python knows how to print "sequences" all at once:

###
>>> print [1, 2, 3, 4]
[1, 2, 3, 4]
>>> print "1234"
1234
###


One of the unique features of the Python 'for' loop is that it works on a
"sequence".  I'm putting the word "sequence" in quotes, because there are
several things in Python that look like sequences:

###
>>> for x in ('larry', 'curly', 'moe'):
...     print 'yuk yuk', x
... 
yuk yuk larry
yuk yuk curly
yuk yuk moe
>>> for x in ['bill', 'ted']:
...    print "excellent", x
... 
excellent bill
excellent ted
>>> for letter in "bee":
...    print 'I spell', letter
... 
I spell b
I spell e
I spell e
###



> In Basic for example I can be certain that what I see when I print is
> what I get internaly. Here it seems that I get one thing when I print
> it and another thing when I try to use it internally in code. Either
> the result of f.readline is a character and so each character should
> print out on a separate line or it is not a character and so it should
> be used as a line in a for loop.


Using a for loop causes Python to look at each element in the "sequence"
individually, so it adds an extra layer between the Thing that we're
looking at, and the process of printing that Thing.  Instead of calling
"print" on the whole sequence,

###
print sequence
###

we now tell Python to "print" every piece, individually.

###
for piece in sequence:
    print piece
###




If it helps, it might help to boil:

###
for line in f.readline():
    print line
###


into a concoction that's almost equivalent, but a little more bland and
wordier:

###
some_line = f.readline()          ## f.readline() sucks in an entire line.
for character in some_line:       ## I renamed "line" to character.
    print character
###

That is, the for loop above is only working on a single line, iterating
over every character of some_line.  Also, note that it only calls
f.readline() once.

[Note: I must be honest: this is only a simplified model of how the for
loop does its work.  As you learn Python, you'll make more accurate models
of what's happening.]




Here are some examples you can try out:

###
for thing in ['last', 'best'] + ['hope', 'for', 'peace']:
    print "Babylon 5: ", thing

for number in range(5):
    print number, number**2, number**3
###

and of course, you should make up your own.  Using the interpreter is
invaluable, as it allows you to directly experiment with the for loop.


Please continue to ask questions on this; we'll do our best to make sure
this makes sense eventually... *grin*