Skip to main content
Engineering LibreTexts

5: Iterations

  • Page ID
    2935
    • 5.1: Updating Variables
      A common pattern in assignment statements is an assignment statement that updates a variable, where the new value of the variable depends on the old.
    • 5.2: The while Statement
      Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that computers do well and people do poorly. Because iteration is so common, Python provides several language features to make it easier.
    • 5.3: Infinite Loops
      An endless source of amusement for programmers is the observation that the directions on shampoo, "Lather, rinse, repeat," are an infinite loop because there is no iteration variable telling you how many times to execute the loop.
    • 5.4: "Infinite loops" and break
      Sometimes you don't know it's time to end a loop until you get half way through the body. In that case you can write an infinite loop on purpose and then use the break statement to jump out of the loop.
    • 5.5: Finishing iterations with continue
      Sometimes you are in an iteration of a loop and want to finish the current iteration and immediately jump to the next iteration. In that case you can use the continue statement to skip to the next iteration without finishing the body of the loop for the current iteration.
    • 5.6: Definite loops using for
      Sometimes we want to loop through a set of things such as a list of words, the lines in a file, or a list of numbers. When we have a list of things to loop through, we can construct a definite loop using a for statement. We call the while statement an indefinite loop because it simply loops until some condition becomes False, whereas the for loop is looping through a known set of items so it runs through as many iterations as there are items in the set.
    • 5.7: Loop patterns
      Often we use a for or while loop to go through a list of items or the contents of a file and we are looking for something such as the largest or smallest value of the data we scan through.
    • 5.8: Counting and Summing Loops
      For example, to count the number of items in a list, we would write the following for loop:
    • 5.9: Maximum and Minimum Loops
    • 5.10: Debugging
      As you start writing bigger programs, you might find yourself spending more time debugging. More code means more chances to make an error and more places for bugs to hide.
    • 5.E: Iterations (Exercises)
    • 5.G: Iterations (Glossary)