Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 38f7197

Browse files
committed
Documentation for the enumerate() function/type.
This closes SF patch #547162.
1 parent 26dd830 commit 38f7197

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

Doc/lib/libfuncs.tex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,18 @@ \section{Built-in Functions \label{built-in-funcs}}
261261
\var{b}, and \code{0 <= abs(\var{a} \%{} \var{b}) < abs(\var{b})}.
262262
\end{funcdesc}
263263

264+
\begin{funcdesc}{enumerate}{iterable}
265+
Return an enumerate object. \var{iterable} must be a sequence, an
266+
iterator, or some other object which supports iteration. The
267+
\method{next()} method of the iterator returned by
268+
\function{enumerate()} returns a tuple containing a count (from
269+
zero) and the corresponding value obtained from iterating over
270+
\var{iterable}. \function{enumerate} is useful for obtaining an
271+
indexed series: \code{(0, seq[0])}, \code{(1, seq[1])}, \code{(2,
272+
seq[2])}, \ldots.
273+
\versionadded{2.3}
274+
\end{funcdesc}
275+
264276
\begin{funcdesc}{eval}{expression\optional{, globals\optional{, locals}}}
265277
The arguments are a string and two optional dictionaries. The
266278
\var{expression} argument is parsed and evaluated as a Python

Doc/tut/tut.tex

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,6 +2014,49 @@ \section{Dictionaries \label{dictionaries}}
20142014
1
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

20192062
The conditions used in \code{while} and \code{if} statements above can

0 commit comments

Comments
 (0)