Lesson guide
What this Python exercise practices
Compute array sum is a beginner practice lesson that focuses on dsa, problem patterns, edge cases. It is designed to be solved in about 6 minutes with examples, starter code, and test feedback.
Prerequisites
- Python functions
- Loops
- Lists
- Basic edge cases
Difficulty and time
- Level
- Beginner
- Estimated time
- 6 minutes
Related public exercises
Summary
Calculate the sum of all numeric elements in an array.
Problem statement
Given a list of numbers (integers), compute and return the sum of all elements. If the list is empty, return 0. Do not use built-in sum() — implement the accumulation manually to practice iteration and accumulation patterns.
Task
Write a function that returns the sum of elements in a list of numbers. Handle empty lists and negative values.
Examples
Basic example
Input
[1, 2, 3, 4]
Output
10
Explanation
1 + 2 + 3 + 4 = 10
Input format
A single argument: a list of integers, e.g. [1, 2, 3]
Output format
An integer representing the sum of the list. For an empty list, return 0.
Constraints
The list length can be 0 to 10^6. Elements fit into Python int. Time complexity O(n).
Samples
Sample input 0
[5, -2, 7]
Sample output 0
10
Explanation 0
5 + (-2) + 7 = 10
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.