5: Iterations
- Page ID
- 122337
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)- 5.1: Updating Variables
- This page explains Python assignment statements and how to update variables, exemplified by incrementing a variable (`x = x + 1`). It highlights the necessity of initializing variables, using `x = 0` before incrementing as an illustration. The concepts of "increment" and "decrement" are also introduced, defining them as adding or subtracting 1 from a variable.
- 5.2: The while Statement
- This page discusses how Python’s while statement aids in automating repetitive tasks by allowing code execution based on a specified condition. It illustrates this with a countdown program that highlights the loop's execution structure, which involves evaluating a condition and repeating code until the condition is false. The importance of the iteration variable in modifying the condition to prevent infinite loops is also emphasized.
- 5.3: Infinite Loops
- This page humorously examines shampoo instructions as an infinite loop due to the lack of an iteration variable, contrasting it with countdown loops that guarantee termination. It emphasizes the difference between loops that are definitely infinite and those that will eventually conclude.
- 5.4: "Infinite loops" and break
- This page covers the implementation of infinite loops in Python using the `while True` statement, emphasizing how to create and exit these loops with the `break` statement under specific conditions. It provides an example in which the loop prompts for user input until "done" is entered, showcasing a practical method for managing loop termination while ensuring clear exit criteria through flexible condition checking.
- 5.5: Finishing iterations with continue
- This page explains the `continue` statement in loops, illustrating how it skips the current iteration and moves to the next. An example shows a loop collecting user input, printing it unless it starts with a hash (indicating a comment), and stopping when "done" is typed. When input begins with a hash, the `continue` statement is activated, resulting in skipping the print for that iteration while the loop proceeds. This behavior is effectively demonstrated in the provided example.
- 5.6: Definite loops using for
- This page explains the use of for loops in Python, which iterate over a defined set of items like lists. It outlines how they differ from while loops, emphasizing that for loops run a fixed number of times based on item count. The syntax involves the keywords "for" and "in," along with an iteration variable. An example is provided to illustrate iterating through a list of friends and printing messages for each, highlighting that the loop concludes after all items are processed.
- 5.7: Loop patterns
- This page explains how to use for and while loops to iterate through lists or file contents to identify specific values like the largest or smallest. It describes the typical structure of these loops, including variable initialization, computations during the iteration, and post-loop examination of results. An example with a list of numbers will further illustrate these concepts and loop patterns.
- 5.8: Counting and Summing Loops
- This page discusses using loops in Python to count items in a list and calculate their sum through a counting loop and a summing loop, respectively. It highlights the process of using a count variable and an accumulator variable. However, it points out that these methods are not very practical since Python offers built-in functions like len() and sum() that achieve the same results more efficiently.
- 5.9: Maximum and Minimum Loops
- This page explains how to find the largest and smallest values in a list using loops in Python, setting initial variables `largest` and `smallest` to `None` and updating them through iteration. It also highlights Python's built-in functions `max()` and `min()` for simplifying these tasks, with examples provided for finding the minimum value.
- 5.10: Debugging
- This page discusses "debugging by bisection," a method for reducing debugging time in large programs. Instead of sequentially checking each line, the coder splits the code in half and tests the midpoint to identify the faulty segment more efficiently. This technique significantly reduces the number of checks needed, though finding an effective midpoint can be challenging, necessitating a focus on likely error-prone areas.
- 5.E: Iterations (Exercises)
- This page presents two programming exercises: the first focuses on collecting numbers, calculating the total, count, and average while managing invalid inputs; the second requires the same approach but outputs the maximum and minimum values instead of the average. Both exercises emphasize input validation and basic list operations.
- 5.G: Iterations (Glossary)
- This page defines key programming concepts related to loops, including accumulators for summing results, counters for recording occurrences, and the processes of incrementing and decrementing variable values. It discusses variable initialization and highlights infinite loops, where the exit condition is unmet. Additionally, it explains iteration as the repeated execution of statements via recursion or looping.