Mailing List Archive

python/dist/src/Doc/tut tut.tex,1.159,1.160
Update of /cvsroot/python/python/dist/src/Doc/tut
In directory usw-pr-cvs1:/tmp/cvs-serv6261/tut

Modified Files:
tut.tex
Log Message:
Documentation for the enumerate() function/type.
This closes SF patch #547162.


Index: tut.tex
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/tut/tut.tex,v
retrieving revision 1.159
retrieving revision 1.160
diff -C2 -d -r1.159 -r1.160
*** tut.tex 8 Mar 2002 00:54:43 -0000 1.159
--- tut.tex 26 Apr 2002 20:29:44 -0000 1.160
***************
*** 2015,2018 ****
--- 2015,2061 ----
\end{verbatim}

+
+ \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}
+
+ When looping through a sequence, the position index and corresponding
+ value can be retrieved at the same time using the
+ \function{enumerate()} function.
+
+ \begin{verbatim}
+ >>> for i, v in enumerate(['tic', 'tac', 'toe']):
+ ... print i, v
+ ...
+ 0 tic
+ 1 tac
+ 2 toe
+ \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}}