Sum of Array
Add a fixed list of integers using a loop. Students trace the accumulator's initial value, repeated updates, and a final print after the complete traversal.
1# Array2arr = [10, 20, 30, 40, 50]34# Calculate sum5sum = 06for element in arr:7 sum += element89print(f"Sum: {sum}")
Output
Sum: 150
In this one
- Accumulate list values
- Initialize a neutral total
- Separate data from input
How it works
- 1
Start with the provided list and sum zero.
- 2
Add every element to sum.
- 3
Print the total after traversal.
Edge cases
What if the list were empty?
What if the list were empty?
The total would remain zero.
Are repeated values added repeatedly?
Are repeated values added repeatedly?
Yes. Every position contributes.
What's going on
The list is already provided, so no input prompts are needed. sum starts at zero and each element is added once.
The running totals are ten, thirty, sixty, one hundred, and one hundred fifty. The final print is outside the loop, so only the completed total appears. Naming a variable sum shadows the built-in function; it does not call that function.
What if the list were empty? The total would remain zero.
Are repeated values added repeatedly? Yes. Every position contributes.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What is the sample total?
Pick an answer to check it.
Question 2
What if the list were empty?
Pick an answer to check it.
Questions people ask
What if the list were empty?
The total would remain zero.
Are repeated values added repeatedly?
Yes. Every position contributes.
What is the sample total?
150.
๐Related Content
- ๐
Phase 4 - Learn Concepts
Review Phase 4 concepts and explanations
- ๐
Phase 4 - Quiz
Test your Phase 4 understanding with quiz questions
- ๐ป
Phase 4 - All Practice Problems
Explore all practice problems for Phase 4
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum