[Python-checkins] python/dist/src/Doc/tut tut.tex,1.156.4.1.2.11,1.156.4.1.2.12

rhettinger@users.sourceforge.net rhettinger@users.sourceforge.net
Fri, 04 Oct 2002 22:14:21 -0700


Update of /cvsroot/python/python/dist/src/Doc/tut
In directory usw-pr-cvs1:/tmp/cvs-serv9662

Modified Files:
      Tag: release22-maint
	tut.tex 
Log Message:
Backport 1.160 on looping idioms.  Excludes enumerate(), a Py2.3 feature.

Index: tut.tex
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/tut/tut.tex,v
retrieving revision 1.156.4.1.2.11
retrieving revision 1.156.4.1.2.12
diff -C2 -d -r1.156.4.1.2.11 -r1.156.4.1.2.12
*** tut.tex	5 Oct 2002 05:04:07 -0000	1.156.4.1.2.11
--- tut.tex	5 Oct 2002 05:14:19 -0000	1.156.4.1.2.12
***************
*** 2047,2050 ****
--- 2047,2079 ----
  
  
+ \section{Looping Techniques \label{loopidioms}}
+ 
+ When looping through dictionaries, the key and corresponding value can
+ be retrieved at the same time using the \method{items()} method.
+ 
+ \begin{verbatim}
+ >>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
+ >>> for k, v in knights.items():
+ ...     print k, v
+ ...
+ gallahad the pure
+ robin the brave
+ \end{verbatim}
+  
+ To loop over two or more sequences at the same time, the entries
+ can be paired with the \function{zip()} function.
+ 
+ \begin{verbatim}
+ >>> questions = ['name', 'quest', 'favorite color']
+ >>> answers = ['lancelot', 'the holy grail', 'blue']
+ >>> for q, a in zip(questions, answers):
+ ...     print 'What is your %s?  It is %s.' % (q, a)
+ ...	
+ What is your name?  It is lancelot.
+ What is your quest?  It is the holy grail.
+ What is your favorite color?  It is blue.
+ \end{verbatim}
+ 
+ 
  \section{More on Conditions \label{conditions}}