Menu

Sign in to track your progress and unlock all features.

Theme style

Log in
01. Print List ItemsE02. Sum List NumbersE03. Count List ElementsE

Problem No 2

Sum List Numbers

Easy

8 minute session

Summary

Use a loop to accumulate the sum of numbers in a list.

Problem statement

Write a function sum_list(nums) that receives a list of numbers (ints or floats) and returns their sum. If the list is empty, return 0. Implement the summation using a loop (for or while) and an accumulator variable — do not use Python's built-in sum() in your implementation.

Task

Implement summation by iterating through a list and accumulating a total without using the built-in sum().

Examples

Simple sum

Input

sum_list([1, 2, 3, 4])

Output

10

Explanation

1 + 2 + 3 + 4 = 10

Input format

A single list named nums passed as the function argument.

Output format

A single numeric value (int or float) equal to the sum of the list elements.

Constraints

- nums length may be 0 or more. - Elements are numbers (int or float). - Use a loop and accumulator; don't use sum() in starter implementation.

Samples

Sample input 0

sum_list([0, -1, 5])

Sample output 0

4

Explanation 0

0 + (-1) + 5 = 4

Code editor
Loading editor…

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.

12:41 PM

Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.