For loops
Iterate over values and build a running total.
On this page
The concept
A for loop obtains one item at a time from an iterable and binds it to the loop variable. Strings provide characters; range provides a sequence of integers. range(start, stop, step) excludes stop, so range(1, 6) supplies 1 through 5. A negative step counts downward.
An accumulator starts with a suitable initial value and updates once per item. For a sum, the initial value is usually zero. Learning this mechanism prepares you to read more concise built-in operations such as sum without treating them as magic.
Worked example
for number in range(1, 6):
print(number)
for letter in "Python":
print(letter)
total = 0
for number in range(2, 11, 2):
total += number
print("Sum of even numbers:", total)
for number in range(3, 0, -1):
print("Countdown:", number)Trace the result
- The first loop displays five integers because 6 is excluded.
- The character loop visits the letters of Python rather than their indexes.
- The even-number loop adds 2, 4, 6, 8, and 10 to total, and the last loop counts down from 3.
1
2
3
4
5
P
y
t
h
o
n
Sum of even numbers: 30
Countdown: 3
Countdown: 2
Countdown: 1Line-by-line walkthrough
Lines 1–2: for number in range(1, 6):
Obtain each item from range(1, 6) and bind it to number before executing the body. A tuple-shaped target unpacks that item. A loop else, if present, runs only when completion does not use break.
Line 2: print(number)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Lines 3–4: for letter in "Python":
Obtain each item from 'Python' and bind it to letter before executing the body. A tuple-shaped target unpacks that item. A loop else, if present, runs only when completion does not use break.
Line 4: print(letter)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 5: total = 0
Evaluate 0, then bind or update total with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.
Lines 6–7: for number in range(2, 11, 2):
Obtain each item from range(2, 11, 2) and bind it to number before executing the body. A tuple-shaped target unpacks that item. A loop else, if present, runs only when completion does not use break.
Line 7: total += number
Update the current target using total += number. Trace its value before and after this line; later statements see the updated state.
Line 8: print("Sum of even numbers:", total)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Lines 9–10: for number in range(3, 0, -1):
Obtain each item from range(3, 0, -1) and bind it to number before executing the body. A tuple-shaped target unpacks that item. A loop else, if present, runs only when completion does not use break.
Line 10: print("Countdown:", number)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Common mistake
Off-by-one errors occur when a mathematical inclusive endpoint is copied directly into range. Write out the intended sequence first.