Keep a running total
Quick review (2 minutes)
Picture a cashier starting a sale at 0.
A running total or a count is set to its starting value before the loop and changed inside the body, so each pass builds on the last. The biggest value so far starts with the first value and is replaced inside the loop when an if finds a bigger one. x += 1 adds 1 to x.
Decide what to keep: a total, a count or the biggest value.
Before the loop, give it a starting value: 0 for a total or a count, the first value for the biggest. A line such as
total = total + stepsreads the name before it changes it, so the name needs a value first.Inside the body, change it on the passes that need it: add each value to a total, add 1 when a pass matches, or replace the biggest when an
iffinds a bigger value.After the loop, use the result: display it, or, when the count is above 0, divide the total by the count for an average.
Trace two passes by hand and check that the value after each pass builds on the one before.
A total set to 0 at the top of the body starts again on every pass. With total = 0 as the body's first line, the step program would display Total: 4000, the last day's steps alone, and Average: 1000.0, that one day divided by 4. Put the starting value above the for line.
Starting the biggest value at 0 goes wrong when every value is below 0: no value is bigger than 0, so the program would display 0, a number that was never read. Start with the first value instead, as the worked example does.
1 In plain words
Picture a cashier starting a sale at 0. Each item adds its price, and the screen shows the total so far.
A loop can keep a running total in the same way. A name is set to 0 before the loop, and each pass adds one value to it. After the loop, the name holds the sum of every value.
The same plan keeps a count: start at 0 and add 1 on each pass that matches. It also keeps the biggest value so far: start with the first value, and replace it on any pass that finds a bigger one.
Remember one sentence: set the total before the loop and add to it inside. To look up the short form count += 1, see section 7.2.1 of the Python 3.13 language reference, on augmented assignment.
2 Picture it
A table of the four passes of the step-count program. The total starts at 0. After the first pass it is 4000, after the second 10500, after the third 16000 and after the fourth 20000. The values are the input of the first worked example. The last row is marked.
3 Why
The line total = total + steps works out the right side first, with the total so far, and then gives the name total the new value. Each pass builds on the value the last pass left.
The total needs a value before the first line that reads it, so the programs here set it above the loop. A line total = 0 at the top of the body would wipe out the total at the start of each pass, and only the last value would be left at the end.
An average is the total divided by the number of values. Dividing with / gives a float, so 20000 / 4 displays as 5000.0, with a decimal point. The short form count += 1 adds 1 to count and gives it the new value, much like count = count + 1.
- Python Software Foundation, Python 3.13 Tutorial, 3.2 First Steps Towards Programming
- Python Software Foundation, Python 3.13 Language Reference, 8.3 The for statement
- Python Software Foundation, Python 3.13 Tutorial, 3.1.1 Numbers
- Python Software Foundation, Python 3.13 Language Reference, 7.2.1 Augmented assignment statements
4 The rule
A running total or a count is set to its starting value before the loop and changed inside the body, so each pass builds on the last.
The biggest value so far starts with the first value and is replaced inside the loop when an if finds a bigger one. x += 1 adds 1 to x.
5 How
- Decide what to keep: a total, a count or the biggest value.
- Before the loop, give it a starting value: 0 for a total or a count, the first value for the biggest. A line such as
total = total + stepsreads the name before it changes it, so the name needs a value first. - Inside the body, change it on the passes that need it: add each value to a total, add 1 when a pass matches, or replace the biggest when an
iffinds a bigger value. - After the loop, use the result: display it, or, when the count is above 0, divide the total by the count for an average.
- Trace two passes by hand and check that the value after each pass builds on the one before.
6 Worked example
Add up four days of steps
total = 0
for day in range(4):
steps = int(input())
total = total + steps
average = total / 4
print(f"Total: {total}")
print(f"Average: {average}")
Standard input
4000
6500
5500
4000
Standard output
Total: 20000
Average: 5000.0
Type the four numbers one a line, or follow the table above. Each pass reads one number and adds it to the total. The average is worked out in a name of its own before it is displayed.
Keep the biggest score so far
biggest = int(input())
for turn in range(3):
score = int(input())
if score > biggest:
biggest = score
print(biggest)
Standard input
12
40
33
40
Standard output
40
The first score, 12, is the biggest so far. 40 replaces it. 33 and the second 40 are not bigger than 40, so biggest does not change, though score does.
Count the letter a
count = 0
for ch in "banana":
if ch == "a":
count += 1
print(count)
Standard input
Empty
Standard output
3
The count goes up by 1 only on the passes where the character is a, because the if runs its block only then. banana has three of them.
- Python Software Foundation, Python 3.13 Library Reference, Built-in Functions: int
- Python Software Foundation, Python 3.13 Library Reference, Built-in Functions: input
- Python Software Foundation, Python 3.13 Tutorial, 3.2 First Steps Towards Programming
- Python Software Foundation, Python 3.13 Tutorial, 3.1.1 Numbers
- Python Software Foundation, Python 3.13 Language Reference, 8.1 The if statement
- Python Software Foundation, Python 3.13 Language Reference, 7.2.1 Augmented assignment statements
Trace
| Step | score | biggest | Explanation |
|---|---|---|---|
| Before the loop | — | 12 | The first number read is the biggest so far. |
| Pass 1 | 40 | 40 | 40 > 12 is True, so biggest becomes 40. |
| Pass 2 | 33 | 40 | 33 > 40 is False, so biggest stays 40. |
| Pass 3 | 40 | 40 | 40 > 40 is False, so biggest stays 40. |
print(biggest) | 40 | 40 | After the loop, the line at the margin displays 40. |
7 The trap
A total set to 0 at the top of the body starts again on every pass. With total = 0 as the body's first line, the step program would display Total: 4000, the last day's steps alone, and Average: 1000.0, that one day divided by 4. Put the starting value above the for line.
Starting the biggest value at 0 goes wrong when every value is below 0: no value is bigger than 0, so the program would display 0, a number that was never read. Start with the first value instead, as the worked example does.
Quick checks
Quick check 1 of 2
Predict what this program displays. The program reads no input.
total = 0
for n in range(2, 11, 2):
total = total + n
print(total)Show the steps and the answer
It displays 30.
Why: The range gives 2, 4, 6, 8 and 10. Adding them one a pass gives 2, 6, 12, 20 and then 30.
Source: Python Software Foundation, Python 3.13 Library Reference, Built-in Types: Ranges
Quick check 2 of 2
Predict what this program displays. The program reads no input.
for n in range(1, 5):
total = 0
total = total + n
print(total)Show the steps and the answer
It displays 4.
Why: Each pass sets total to 0 and then adds that pass's number. After the last pass, with n holding 4, total is 4.
Source: Python Software Foundation, Python 3.13 Language Reference, 8.3 The for statement
Sources
- Python 3.13 Tutorial, 3.2 First Steps Towards Programming. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
- Python 3.13 Language Reference, 8.1 The if statement. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
- Python 3.13 Language Reference, 7.2.1 Augmented assignment statements. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
- Python 3.13 Language Reference, 8.3 The for statement. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
- Python 3.13 Tutorial, 3.1.1 Numbers. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
- Python 3.13 Library Reference, Built-in Functions: int. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
- Python 3.13 Library Reference, Built-in Functions: input. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
- Python 3.13 Library Reference, Built-in Types: Ranges. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.