0
0
Pythonprogramming~20 mins

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

Choose your learning style9 modes available
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.