@@ -2014,6 +2014,49 @@ \section{Dictionaries \label{dictionaries}}
201420141
20152015\end {verbatim }
20162016
2017+
2018+ \section {Looping Techniques \label {loopidioms } }
2019+
2020+ When looping through dictionaries, the key and corresponding value can
2021+ be retrieved at the same time using the \method {items()} method.
2022+
2023+ \begin {verbatim }
2024+ >>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
2025+ >>> for k, v in knights.items():
2026+ ... print k, v
2027+ ...
2028+ gallahad the pure
2029+ robin the brave
2030+ \end {verbatim }
2031+
2032+ When looping through a sequence, the position index and corresponding
2033+ value can be retrieved at the same time using the
2034+ \function {enumerate()} function.
2035+
2036+ \begin {verbatim }
2037+ >>> for i, v in enumerate(['tic', 'tac', 'toe']):
2038+ ... print i, v
2039+ ...
2040+ 0 tic
2041+ 1 tac
2042+ 2 toe
2043+ \end {verbatim }
2044+
2045+ To loop over two or more sequences at the same time, the entries
2046+ can be paired with the \function {zip()} function.
2047+
2048+ \begin {verbatim }
2049+ >>> questions = ['name', 'quest', 'favorite color']
2050+ >>> answers = ['lancelot', 'the holy grail', 'blue']
2051+ >>> for q, a in zip(questions, answers):
2052+ ... print 'What is your %s? It is %s.' % (q, a)
2053+ ...
2054+ What is your name ? It is lancelot .
2055+ What is your quest ? It is the holy grail .
2056+ What is your favorite color ? It is blue .
2057+ \end {verbatim }
2058+
2059+
20172060\section {More on Conditions \label {conditions } }
20182061
20192062The conditions used in \code {while} and \code {if} statements above can
0 commit comments