Hello Richard! On Thursday January 7 2010 13:43:26 Richard D. Moores wrote: > On p. 162 of "Programming In Python", 2nd ed., by Summerfield, the > section entitled "for Loops" begins: > > ========================================= > for expression in iterable: > for_suite > else: > else_suite > > The expression is normally either a single variable or a sequence of > variables, usually in the form of a tuple. If a tuple or list is used > for the expression, each item is unpacked into the expression's items. > ====================================== > > I thought I was quite familiar with for loops, but I don't understand > how the expression can be a sequence of variables, nor what unpacking > into the expression's items means. Could someone explain this, > preferably with an example?
Your book seems to be a bit sloppy with the terms it chooses for describing the syntax. Look at the official syntax description section 7.3: http://docs.python.org/reference/compound_stmts.html#the-for-statement After the for statement there is no expression, but something called target_list. This target_list normally contains unknown variables, which are illegal in expressions. The whole complicated wording refers to this case: >>> for a,b,c in ((1,2,3), (4,5,6), (10,20,30)): ... print a,b,c ... 1 2 3 4 5 6 10 20 30 Unpacking is the operation done in: a,b,b = (2,4,5) The "a,b,c" on the left side of the assignment operator is also a target list. See section 6.2: http://docs.python.org/reference/simple_stmts.html#assignment-statements Eike. _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor