Editor

Sum of List

Lists · Beginner · 40 XP · about 4 min

Write a function called sum_list(numbers) that returns the sum of all numbers in a list.

Challenge: Try to solve this without using Python's built-in sum() function!

Example

sum_list([1, 2, 3, 4, 5])  # Returns 15
sum_list([10, -5, 3])      # Returns 8
sum_list([])               # Returns 0

Requirements

  • Return the sum of all numbers
  • Handle empty lists (return 0)
  • Handle negative numbers
def sum_list(numbers):
# Your code here - try without using sum()!
pass
# Test your function
print(sum_list([1, 2, 3, 4, 5]))
print(sum_list([10, -5, 3]))

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.