liveBook · Manning

Repetition

As explained in Get Programming: Learn to code with Python

Repetition is the performance of a task or sequence of steps more than once.

Repetition means performing a task or sequence of steps more than once. In programming, it appears both as repeating string values and as repeating commands with loops. section 8.2 section 16.1

Recognize repetition in two common forms:

  • String repetition multiplies a string by an integer: 3 * "a" produces "aaa". Writing "-" * 10 creates ten unknown-letter markers without typing each character. section 8.2
  • Task repetition uses loops to execute a set of statements many times, possibly changing a value on each pass. section 16.1
  • Every repetition needs a clear stopping rule. “Lather, rinse, repeat” can continue forever, while “repeat as needed” checks whether another pass is required. section 16.1
  • A recipe or flowchart can show repetition by directing an arrow back to the first step in the repeated sequence. section 2.2

Repeating a command three times

for v in range(3): print("var v is", v) Python assigns 0, then 1, then 2 to v and runs the indented print statement after each assignment. The loop therefore performs the statement three times. You can replace 3 with a variable such as n_times when the repetition count changes. section 16.3

Listing 16.2. A for loop that prints the loop variable value
for v in range(3):               #1
    print("var v is", v)         #2

Inspect the compact for-loop listing, then compare it with the unrolled version to trace each repeated execution. section 16.3

How Get Programming: Learn to code with Python covers it

How deep it goes
Explains string multiplication, loop execution, termination, range(N), and unrolling, but stops short of broader loop varieties.
Examples
Uses hangman markers, greetings, shampoo instructions, bread recipes, and a range(3) loop printing 0, 1, and 2.
What it assumes
Builds from basic strings, integers, statements, and step-by-step execution rather than assuming advanced programming knowledge.
What you can do after
Apply repetition via Python string multiplication and for loops with explicit counts and stopping conditions.
How it connects
Connects repetition with strings, integer multiplication, loops, for loops, range, flowcharts, and infinite loops.

Also explained in

Compare how 2 books explain repetition →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.