10-count-controlled-iteration.py
python-programming / programming-constructs  ·  Lesson 11
Lesson 11 — Programming constructs

Iteration: Count-Controlled

Repeat a block a known number of times with for and range().

A count-controlled loop is suitable when the number of repetitions is known. In Python this is commonly written with for and range().

Predict

Read the code first. Do not run it yet.

count-controlled-iteration-predict.py
total = 0
for i in range(1, 5):
    total = total + i
print(total)
Predict before running
  • Which values will i take?
  • How many times does the loop run?
  • Predict the final value of total.

Investigate

Use this code to explain exactly how the program works.

count-controlled-iteration-investigate.py
for number in range(2, 11, 2):
    print(number)
Investigate the code
  • What are the start, stop and step values?
  • Why is 11 not printed?
  • How would you change the code to print 3, 6, 9, 12?

Using range()

FormValues produced
range(5)0, 1, 2, 3, 4
range(2, 6)2, 3, 4, 5
range(2, 11, 2)2, 4, 6, 8, 10
repeat.py
for count in range(3):
    print("Practice")
Output
Practice
Practice
Practice
The stop value is excludedrange(start, stop) includes the start value but stops before the stop value.

Self-study guide

Count-controlled iteration repeats a block a known number of times. Python normally uses a for loop with range(). OCR Exam Reference Language writes an inclusive start and end value, while Python range(start, stop) excludes the stop value.

This end-point difference is one of the most important OCR-to-Python translations. OCR for i=0 to 9 runs ten times; Python needs range(0, 10) to produce the same values.

OCR focusUnderstand and use count-controlled loops. Be able to translate inclusive OCR FOR ranges into Python range() correctly, including a step value when needed.

OCR Exam Reference Language ↔ Python

OCR Exam Reference Language / termPythonWhat to remember
for i=0 to 9 ... next ifor i in range(0, 10):OCR end 9 is inclusive; Python stop 10 is excluded.
for i=2 to 10 step 2for i in range(2, 11, 2):Use stop 11 so 10 can be produced.
for i=10 to 0 step -1for i in range(10, -1, -1):Use stop -1 so 0 can be produced.
Read both formsIn an examination you may see OCR Exam Reference Language even when you have learned the idea in Python. Translate the structure, not just the symbols.

Worked example

A running total is a common use of count-controlled iteration.

lesson-11-worked.py
total = 0

for day in range(1, 6):
    minutes = int(input("Minutes: "))
    total = total + minutes

print(total)
Output
150
Work through it line by line
  • range(1, 6) produces 1, 2, 3, 4, 5: five repetitions.
  • total is created before the loop so it keeps its value between repetitions.
  • Each new input is added to the previous total.
  • The final print() is not indented because it should run once after the loop.

Another example

A step value changes the loop variable by more than one each time.

lesson-11-example-2.py
for number in range(2, 11, 2):
    print(number)
Output
2
4
6
8
10
Why this example matters
  • The start is 2.
  • The stop value 11 is excluded.
  • The step 2 gives every second integer.

Common mistakes

Avoid these errors
  • Using range(1, 5) when you intend to include 5.
  • Indenting code that should run after the loop.
  • Changing the loop variable manually when range() is already controlling it.
  • Forgetting that OCR FOR endpoints are inclusive but Python range stops are exclusive.
Exam-safe warningTranslate OCR inclusive endpoints carefully. OCR 0 to 9 corresponds to Python range(0, 10).

Exam-ready checklist

You should now be able to
  • Choose a count-controlled loop when the repetition count is known.
  • Calculate the exact values generated by range().
  • Translate OCR FOR loops to Python and back.
  • Use a loop variable, running total and counter correctly.

What you have learned

  • Count-controlled iteration repeats a known number of times.
  • Use for with range() for numeric counts.
  • The stop value in range() is not included.