[Tutor] One of my 'baby step' programs
John Fouhy
john at fouhy.net
Mon Oct 2 22:40:27 CEST 2006
On 03/10/06, Alan Gilfoy <agilfoy at frontiernet.net> wrote:
>
> >>>> for i in range(10):
> > ... break
> > ... else:
> > ... print 'foo'
> > ...
> >>>> for i in range(10):
> > ... pass
> > ... else:
> > ... print 'foo'
> > ...
> > foo
> >>>>
>
> pardon the newb question, but what do these code lines do?
They demonstrate when the else: clause in a for statement gets executed.
Spaces sometimes get lost in email, so I'll write them again, with
underscores instead of spaces:
for i in range(10):
____break
else:
____print 'foo'
In this case, the for loop will exit because of the break statement,
and so the else: clause will not execute.
for i in range(10):
____pass
else:
____print 'foo'
In this case, the loop body is just 'pass', which is python's
do-nothing statement. So the loop will exit normally, and the else:
clause will execute (resulting in the string 'foo' being printed).
Does this help?
--
John.
More information about the Tutor
mailing list