Bird
Raised Fist0
Pythonprogramming~20 mins

sum() function in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
๐ŸŽ–๏ธ
Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Sum of a list with a start value
What is the output of this code?
numbers = [1, 2, 3, 4]
result = sum(numbers, 10)
print(result)
Python
numbers = [1, 2, 3, 4]
result = sum(numbers, 10)
print(result)
A24
B10
CTypeError
D20
Attempts:
2 left
๐Ÿ’ก Hint
Remember that sum() can take a second argument as the starting value.
โ“ Predict Output
intermediate
2:00remaining
Sum of a tuple without start value
What is the output of this code?
values = (5, 10, 15)
print(sum(values))
Python
values = (5, 10, 15)
print(sum(values))
ATypeError
B50
C30
D15
Attempts:
2 left
๐Ÿ’ก Hint
sum() adds all numbers in the tuple by default starting from zero.
โ“ Predict Output
advanced
2:00remaining
Sum of a list of strings
What error does this code raise?
words = ['a', 'b', 'c']
print(sum(words))
Python
words = ['a', 'b', 'c']
print(sum(words))
AValueError
BTypeError
CSyntaxError
D'' (empty string)
Attempts:
2 left
๐Ÿ’ก Hint
sum() works only with numbers by default.
โ“ Predict Output
advanced
2:00remaining
Sum of a list with a non-zero start value and negative numbers
What is the output of this code?
nums = [-1, -2, -3]
print(sum(nums, 5))
Python
nums = [-1, -2, -3]
print(sum(nums, 5))
A-1
B5
CTypeError
D-6
Attempts:
2 left
๐Ÿ’ก Hint
sum() adds all numbers starting from the start value.
๐Ÿง  Conceptual
expert
2:00remaining
Sum() with a list of lists
What error does this code raise?
list_of_lists = [[1, 2], [3, 4]]
print(sum(list_of_lists))
Python
list_of_lists = [[1, 2], [3, 4]]
print(sum(list_of_lists))
ATypeError
BValueError
CSyntaxError
D[1, 2, 3, 4]
Attempts:
2 left
๐Ÿ’ก Hint
sum() tries to add lists using + operator but needs a start value for lists.

Practice

(1/5)
1. What does the sum() function do in Python?
easy
A. Counts the number of elements in an iterable
B. Multiplies all numbers in an iterable
C. Adds all numbers in an iterable and returns the total
D. Finds the largest number in an iterable

Solution

  1. Step 1: Understand the purpose of sum()

    The sum() function takes an iterable like a list or tuple and adds all its numbers together.
  2. Step 2: Compare options with the function's behavior

    Only Adds all numbers in an iterable and returns the total correctly describes adding all numbers and returning the total.
  3. Final Answer:

    Adds all numbers in an iterable and returns the total -> Option C
  4. Quick Check:

    sum() adds numbers [OK]
Hint: Remember: sum() adds, not multiplies or counts [OK]
Common Mistakes:
  • Confusing sum() with multiplication
  • Thinking sum() counts elements
  • Assuming sum() finds max value
2. Which of the following is the correct syntax to sum numbers in a list nums starting from 10?
easy
A. sum(nums, 10)
B. sum(10, nums)
C. sum(nums + 10)
D. sum(start=10, nums)

Solution

  1. Step 1: Recall the sum() function signature

    The sum() function syntax is sum(iterable, start=0), where the second argument is the starting value.
  2. Step 2: Identify correct argument order

    sum(nums, 10) uses sum(nums, 10), which correctly passes the iterable first and start second.
  3. Final Answer:

    sum(nums, 10) -> Option A
  4. Quick Check:

    sum(iterable, start) = sum(nums, 10) [OK]
Hint: Iterable first, start second in sum() [OK]
Common Mistakes:
  • Swapping the order of arguments
  • Using plus sign inside sum()
  • Using keyword argument incorrectly
3. What is the output of the following code?
numbers = [2, 4, 6]
result = sum(numbers, 5)
print(result)
medium
A. Error
B. 12
C. 11
D. 17

Solution

  1. Step 1: Calculate sum of list elements

    The list numbers contains 2, 4, and 6. Their sum is 2 + 4 + 6 = 12.
  2. Step 2: Add the start value

    The start value is 5, so total is 12 + 5 = 17.
  3. Final Answer:

    17 -> Option D
  4. Quick Check:

    sum([2,4,6],5) = 17 [OK]
Hint: Add start value after summing list [OK]
Common Mistakes:
  • Forgetting to add the start value
  • Adding start value before summing
  • Assuming sum returns list length
4. The code below throws an error. What is the problem?
values = [1, 2, '3', 4]
total = sum(values)
medium
A. sum() requires a start argument
B. List contains a string which cannot be added
C. sum() only works with tuples
D. Missing parentheses in sum() call

Solution

  1. Step 1: Check list element types

    The list values contains integers and a string '3'. sum() cannot add strings to numbers.
  2. Step 2: Understand sum() type requirements

    sum() requires all elements to be numbers. Mixing types causes a TypeError.
  3. Final Answer:

    List contains a string which cannot be added -> Option B
  4. Quick Check:

    sum() needs all numbers [OK]
Hint: Ensure all list items are numbers before sum() [OK]
Common Mistakes:
  • Ignoring mixed data types
  • Thinking sum() needs start argument
  • Assuming sum() works on strings
5. You have a list of daily sales: sales = [100, 200, 0, 150, 300]. You want to calculate the total sales but ignore days with zero sales. Which code correctly uses sum() to do this?
hard
A. sum(sale for sale in sales if sale != 0)
B. sum(sales) - sales.count(0)
C. sum(sales, 0) if 0 not in sales else 0
D. sum(sales) / sales.count(0)

Solution

  1. Step 1: Filter out zero sales using a generator expression

    The expression sale for sale in sales if sale != 0 creates a sequence of sales excluding zeros.
  2. Step 2: Use sum() on filtered sales

    sum() adds only the non-zero sales, giving the correct total.
  3. Final Answer:

    sum(sale for sale in sales if sale != 0) -> Option A
  4. Quick Check:

    sum(filtered sales) = correct total [OK]
Hint: Use sum() with condition inside generator [OK]
Common Mistakes:
  • Subtracting the count of zeros instead of filtering
  • Using sum() with wrong condition
  • Dividing sum by zero count