|
| 1 | +\hsection{Loops}% |
| 2 | +% |
| 3 | +When we are working with sequences of data, we do not just want to perform an action on one data element. |
| 4 | +We instead often want to apply the actions repetitively to all data elements. |
| 5 | +Loops allow us to do just that, to perform the same actions multiple times.% |
| 6 | +\pythonIdx{loop}% |
| 7 | +% |
| 8 | +\hsection{The \texttt{for} Loop Statement}% |
| 9 | +% |
| 10 | +\pythonIdx{loop!for}% |
| 11 | +The most basic such sequence in \python\ may be the \pythonilIdx{for}~loop, which has the following pattern:% |
| 12 | +% |
| 13 | +\begin{pythonSyntax} |
| 14 | +for loopVariable in sequence: |
| 15 | + loop body statement 1 |
| 16 | + loop body statement 2 |
| 17 | + # ... |
| 18 | + |
| 19 | +normal statement 1 |
| 20 | +normal statement 2 |
| 21 | +# ... |
| 22 | +\end{pythonSyntax} |
| 23 | +% |
| 24 | +% |
| 25 | +\gitPythonAndOutput{\programmingWithPythonCodeRepo}{04_loops}{for_loop_range.py}{--args format}{loops:for_loop_range}{% |
| 26 | +An example for using the \pythonilIdx{for} loop over a \pythonilsIdx{range} of integer numbers.}% |
| 27 | +% |
| 28 | +\gitPythonAndOutput{\programmingWithPythonCodeRepo}{04_loops}{for_loop_pi_liu_hui.py}{--args format}{loops:for_loop_pi_liu_hui}{% |
| 29 | +A variant of \cref{lst:variables:pi_liu_hui} which uses a \pythonilIdx{for} loop instead of five copies of the same instructions.}% |
| 30 | +% |
| 31 | +\gitPythonAndOutput{\programmingWithPythonCodeRepo}{04_loops}{for_loop_continue_break.py}{--args format}{loops:for_loop_continue_break}{% |
| 32 | +An example for the \pythonilIdx{continue} and \pythonilIdx{break} statements in a \pythonilIdx{for} loop.}% |
| 33 | +% |
| 34 | +The keyword~\pythonilIdx{for} is followed by a loop variable. |
| 35 | +Then comes the keyword~\pythonilIdx{in}, the \pythonil{sequence} we want to iterate over, and finally a colon~(\pythonilIdx{:}). |
| 36 | +This variable will iteratively take on the values in the \pythonil{sequence}. |
| 37 | +The loop body statements in the following, indented block are executed for each of these values. |
| 38 | +After the loop, we leave a blank line followed by the code that will be executed after the loop completes. |
| 39 | + |
| 40 | +In its most simple form, the \pythonilIdx{for} loop is applied to a \pythonilIdx{range} of integer numbers. |
| 41 | +Ranges are sequences which work basically like slices\pythonIdx{slice} (see \cref{sec:lists:basicFunctions,sec:strBasicOperations}). |
| 42 | +\pythonil{range(5)} will give us a sequence of integers starting with~0 and reaching up to right \emph{before}~5, i.e., the integer range~\intRange{0}{4}. |
| 43 | +\pythonil{range(6, 9)} gives the sequence of integers starting with~6 and stopping right \emph{before}~9, i.e., the integer range~\intRange{6}{8}. |
| 44 | +Finally, \pythonil{range(20, 27, 2)} results in a sequence of integers that begins at~20, increments by~2 in each step, and ends right before~27. |
| 45 | +This is the sequence~$(20, 22, 24, 26)$. |
| 46 | +\pythonilsIdx{range}, like slices\pythonIdx{slice}, can also have negative increments: |
| 47 | +The \pythonil{range(40, 30, -3)} starts with~40 and stops before reaching~30 and decrements by~3 in each step. |
| 48 | +This is equivalent to the set~$(40, 37, 34, 31)$. |
| 49 | + |
| 50 | +In \cref{lst:loops:for_loop_range}, we loop over exactly these ranges. |
| 51 | +In this listing, we try to create a dictionary (see \cref{sec:dictionaries}) where some integer numbers are mapped to their squares. |
| 52 | +We use four \pythonilIdx{for} loops to fill this dictionary with data.% |
| 53 | +% |
| 54 | +\endhsection% |
| 55 | +% |
| 56 | +\endhsection% |
| 57 | +% |
0 commit comments