"for" with "else"?

Alan Kennedy alanmk at hotmail.com
Thu Sep 25 15:57:59 CEST 2003

[Invalid User wrote]
> While trying to print a none empty list, I accidentaly put an "else"
> statement with a "for" instead of "if". Here is what I had:

>From the Python Language Reference 

The for statement 

for_stmt  ::=  "for" target_list "in" expression_list ":" suite 
    ["else" ":" suite] 

The expression list is evaluated once; it should yield a sequence. The
suite is then executed once for each item in the sequence, in the
order of ascending indices. Each item in turn is assigned to the
target list using the standard rules for assignments, and then the
suite is executed. When the items are exhausted (which is immediately
when the sequence is empty), the suite in the else clause, if present,
is executed, and the loop terminates. 


A quick glance through the archives confirms that this has been the
case since at least python 1.4:-



alan kennedy
check http headers here: http://xhaus.com/headers
email alan:              http://xhaus.com/mailto/alan

More information about the Python-list mailing list