1.5.2 for: else:
Thomas Wouters
thomas at xs4all.nl
Tue Jul 27 08:29:43 EDT 1999
On Tue, Jul 27, 1999 at 04:15:44PM +0400, Oleg Broytmann wrote:
> The following program:
> ----------
> for a in ['a', 12]:
> print a
> else:
> print "Empty!"
> ----------
http://www.python.org/doc/current/tut/node6.html#SECTION006200000000000000000
[..]
4.4 break and continue Statements, and else Clauses on Loops
Loop statements may have an else clause; it is executed when the loop
terminates through exhaustion of the list (with for) or when the condition
becomes false (with while), but not when the loop is terminated by a break
statement.
[..]
So,
>>> def do_a(list):
... for a in list:
... if not a:
... break
... print a
... else:
... print "No FALSE items in list!"
...
>>> do_a(['a', 12])
a
12
No FALSE items in list!
>>> do_a(['a', 12, []])
a
12
>>> do_a(['a', 12, 0])
a
12
Else clauses get executed on normal loop exit, even if the loop was 'empty'.
It does not get executed when you 'break' out of a loop.
--
Thomas Wouters <thomas at xs4all.net>
Hi! I'm a .signature virus! copy me into your .signature file to help me spread!
More information about the Python-list
mailing list