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 variable updating in Python, highlighting that new values depend on old ones (e.g., "x = x + 1"). It notes that using a non-existent variable causes a NameError, necessitating prior initialization (e.g., "x = 0"). The concepts of increment (adding 1) and decrement (subtracting 1) are also defined.
- 5.2: The while Statement
- This page explains Python's iteration capabilities, focusing on the while statement that automates repetitive tasks. It runs a code block while a condition is true, demonstrated by counting down from five. The loop continues until the specified condition is false, necessitating variable changes within the loop to prevent infinite loops.
- 5.3: Infinite Loops
- This page humorously critiques shampoo directions as an example of an infinite loop due to the lack of an iteration variable for repetitions. It compares this to a countdown loop, which is finite and guaranteed to terminate. The text emphasizes that certain loops are inherently infinite because they lack such variables.
- 5.4: "Infinite loops" and break
- This page discusses infinite loops in Python programming, created using a while statement with a True condition. It explains that these loops can run indefinitely unless broken with a break statement, providing an example of user input collection that exits the loop when "done" is typed. The text emphasizes flexible condition checking and the importance of a positive exit condition.
- 5.5: Finishing iterations with continue
- This page explains the "continue" statement in loops, which allows skipping to the next iteration without completing the current one. It provides an example where user input is copied, ignoring lines that start with a hash character. When "continue" is executed for these lines, they are skipped, and the program resumes with the next input, effectively demonstrating the functionality through a sample run.
- 5.6: Definite loops using for
- This page explains programming loops, particularly the for loop, which iterates over a known set of items, and contrasts it with the while loop that continues until a condition is false. An example in Python illustrates looping through a list of names, printing greetings for each. The iteration variable, 'friend,' changes in each cycle, guiding the loop's progression. The syntax of the for loop is also described, showcasing its function in relation to the list.
- 5.7: Loop patterns
- This page explains how to use for and while loops to iterate through lists or file contents in order to find specific values, such as the largest or smallest number. It outlines the structure of these loops, emphasizing the importance of initializing variables before the loop, processing each item, and analyzing results after the loop concludes. A practical example with a list of numbers reinforces these concepts and loop patterns.
- 5.8: Counting and Summing Loops
- This page provides examples of two Python loops: one for counting items in a list and another for calculating their total. The counting loop increments a variable to reflect the total number of items, while the total loop sums the items. It highlights that Python's built-in functions `len()` and `sum()` can perform these operations more efficiently.
- 5.9: Maximum and Minimum Loops
- This page explains how to find the largest and smallest values in a list using Python loops, initializing variables to None and updating them during iteration. It mentions using Python's built-in `max()` and `min()` functions as simpler alternatives, and includes a simplified custom `min()` function example without print statements.
- 5.10: Debugging
- This page discusses "debugging by bisection," a method to streamline bug identification in large programs by systematically halving the code to locate errors more efficiently. Instead of examining every line, the midpoint is checked first, narrowing down the error's location with each step. While this method significantly reduces the amount of code to review, determining the midpoint can be challenging, making it important to concentrate on high-probability areas for errors.
- 5.E: Iterations (Exercises)
- This page outlines two programming exercises: Exercise 1 involves creating a program that reads numbers and calculates the total, count, and average while managing invalid inputs. Exercise 2 focuses on collecting numbers to determine the maximum and minimum values instead of the average. The text also indicates that further details about lists will be provided in a later chapter.
- 5.G: Iterations (Glossary)
- This page defines essential programming concepts including accumulators for summing, counters for tallying, and operations such as incrementing, decrementing, initializing, and iterations in loops. It also mentions infinite loops, which lack a terminating condition.