Summarize a Collection with a Loop
Read a list, accumulate values and check an empty case.
By BizAIPro Learning · Prerequisites: Run Your First Python Script Locally
What you will learn
- Create a list of amounts
- Follow an indented loop body
- Explain an accumulator
A list keeps a sequence
A Python list can hold several values in order. For this exercise all entries are numeric amounts using the same unit. Decide what data belongs in the collection before totaling it. Adding unrelated quantities can produce a number with no useful meaning.
Indentation defines the body
A for statement ends with a colon. The indented lines after it run for each item. Unindented lines run after the loop. Consistent spaces are essential because indentation changes the program structure, not just its appearance.
Follow the accumulator
The total starts at 0. Each iteration adds one amount and stores the new result. With 10, 20 and 5, the successive totals are 10, 30 and 35. Printing after the loop displays one final result; printing inside the loop displays each intermediate result.
Try an empty collection
Replace amounts with an empty list and predict the result. Since no iterations run, the initialized total stays at 0. The program still has defined behavior. Also consider invalid entries such as text, which this introductory example does not validate.
Apply the idea
An expense summary uses the same iteration pattern, but production input needs validation and clear currency rules. Keep the first version small and test it locally. Record both the ordinary and empty cases in your project README so the behavior can be checked.
Read the example
amounts = [10, 20, 5]
total = 0
for amount in amounts:
total = total + amount
print(total)Line-by-line explanation
- amounts contains three values.
- total is initialized before the loop.
- The indented assignment runs once per value.
- The final print is outside the loop.
Expected output
35
Common mistakes to avoid
- Indenting the final print unintentionally
- Mixing strings and numbers without validation
- Forgetting to initialize the total
Run this script on your computer
Python is not executed in this browser. Install Python 3 from the official Python website, save the script and run python bizaipro-lesson.py or python3 bizaipro-lesson.py.
amounts = [10, 20, 5]
total = 0
for amount in amounts:
total = total + amount
print(total)Check the expected output in the lesson, then complete the output-prediction assessment below.
Trace an accumulator
For amounts = [10, 20, 5], total starts at 0 and adds each amount. Enter the final printed total. Verify with a local Python script.
What is printed for amounts = []?
Summary & next step
- Can you now create a list of amounts?
- Can you now follow an indented loop body?
- Can you now explain an accumulator?
Complete the exercise and pass the knowledge check to complete this lesson. Guest progress stays on this browser and does not sync across devices.

