The sum of an array (or list) is the result of adding every element once. This is the classic accumulator pattern: start at 0, then total += value for each item.
total = 0
+ 1 → total = 1
+ 2 → total = 3
+ 3 → total = 6
+ 4 → total = 10
+ 5 → total = 15
Sum = 15
Code
Python Programs
Start with a fixed list and a simple helper. Then read values from input, and finish by tracing a running total with negatives.
Example 1 — Sum of a Fixed Array
Use a for loop to add every element into an accumulator.
python
def sum_array(arr: list[int]) -> int:
total = 0
for value in arr:
total += value
return total
array = [1, 2, 3, 4, 5]
print(f"Sum of the array elements: {sum_array(array)}")
Output
Sum of the array elements: 15
How It Works
total starts at 0.
The loop visits 1, 2, 3, 4, then 5.
Each visit does total += value.
The running values are 1, 3, 6, 10, then 15.
Return the final total. Shortcut in real code: sum(array).
Example 2 — Sum with User Input
Read n, fill a list, then reuse the same summing helper.
python
def sum_array(arr: list[int]) -> int:
total = 0
for value in arr:
total += value
return total
n = int(input("Enter number of elements: "))
if n < 0:
raise ValueError("n must be nonnegative.")
arr = []
for i in range(n):
value = int(input(f"Enter element {i + 1}: "))
arr.append(value)
print(f"Sum of the array elements: {sum_array(arr)}")
Output
Enter number of elements: 5
Enter element 1: 1
Enter element 2: 2
Enter element 3: 3
Enter element 4: 4
Enter element 5: 5
Sum of the array elements: 15
How It Works
Read the length n and reject a negative size.
Build a list by appending each typed value.
Call sum_array — input and summing stay separate.
If n is 0, the list is empty and the sum is 0.
Same helper as Example 1, so testing stays easy.
Example 3 — Running Total with Negatives
Print the accumulator after each step so negatives are easy to see.
python
arr = [10, -2, 7]
total = 0
print("Running total:")
for value in arr:
total += value
print(f" after {value:+d}: {total}")
print(f"Final sum: {total}")
Output
Running total:
after +10: 10
after -2: 8
after +7: 15
Final sum: 15
How It Works
Start at 0, then add 10 → 10.
Add -2 → 8 (no special case for negatives).
Add 7 → 15.
{value:+d} shows a sign on each added value.
Tracing the running total is a strong whiteboard habit.
Edge Cases & Pitfalls
Check these before calling the solution done.
Empty
Sum is 0
An empty list never enters the loop, so the accumulator stays at 0.
Negatives
Just add them
Do not take absolute values unless the problem asks for that.
Built-in
sum(arr)
Fine in apps; write the loop yourself in interviews.
None
Guard invalid input
In real code, check for None before looping. Interviews often assume a valid list.
Analysis
Time and Space Complexity
Program
Time
Extra space
for loop / sum()
O(n)
O(1)
User-input build + sum
O(n)
O(n) for the list
Running-total trace
O(n)
O(1)
You must look at every element at least once, so O(n) is optimal for the general case.
Remember
Key Takeaways
Pattern: accumulator starts at 0, then adds each element once.