Repeating and Remembering

Overview


In Part 1, your programs asked for information, stored values in variables, performed calculations, and chose between different branches.

Many programs need to repeat the same kind of work for several values. You might inspect every character in a word, calculate a total from several scores, or keep asking for input until the user decides to stop.

This part begins with something you already know: strings. In Part 1, you received, cleaned up, compared, and printed text values. You will now look inside strings and see that they are sequences of characters. A loop lets you process those characters one at a time. You will then use the same ideas with lists, which are sequences of values that your program can create and change.

The reasoning habits from Part 1 continue. A loop can execute the same lines many times, which makes tracing especially important. When a loop is difficult to understand, do not try to imagine all of its repetitions at once. Follow one iteration, record what changed, and then continue to the next one.

After working through this part, you should be able to write, trace, test, and explain programs that use strings, lists, and loops to work with several values.

The structure of this part is as follows:

  • Strings Are Sequences looks more closely at strings and introduces characters, positions, indexing, length, and membership.
  • Repeating over a Sequence introduces for loops using strings, then shows how range(...) can repeat an action a fixed number of times.
  • Lists: Remembering Several Values introduces lists and shows how they can be inspected, changed, and processed with the same for loop idea.
  • Processing and Changing Lists develops loop patterns for totals, counts, comparisons, and building lists, then shows how to change values by position. A checkpoint separates the two stages.
  • Repeating Until We Decide to Stop introduces while True and break for situations where the amount of input is not known beforehand.
  • Choosing and Combining Repetition brings the loop forms together, introduces .split(), and applies indexes to corresponding and neighboring items.
  • Repetition and Games asks you to write the central loop for a small graphical game while leaving unfamiliar framework code alone.
  • Recap and Feedback reviews the part and includes a rainfall-analysis exercise that reuses several earlier patterns.

A useful rhythm for this part is:

  1. read each chapter in order;
  2. type and run the examples rather than only reading them;
  3. complete the exercises as you go;
  4. when an exercise asks for a prediction, make the prediction before answering;
  5. when a loop’s behavior is not obvious, trace one iteration at a time;
  6. finish with the recap and feedback.

In Chapter 5, use the checkpoint to check your understanding of processing values before moving on to replacing list items. Return to the examples when an exercise reveals something you do not yet understand.

The most important working habit in this part is:

Predict first. Follow one iteration at a time. Run and compare.

A loop is still ordinary Python code. The important difference is that the program returns to the same block and runs it again.

Help us with research

0 / 10 points

Before getting started with the second part, please complete a short survey about your learning goals and course experiences. Your responses can help us better understand how students learn and how course tools and feedback could be improved in the future.

Taking part in the research is voluntary. You may skip survey items you do not want to answer, and you can choose whether or not to allow your survey responses to be used for research. You will receive the course points for completing the survey regardless of that choice.

Click here to take the survey

After completing the survey, return here and enter the code shown at the end. Completing the survey grants you a small amount of course points.