Recall & Review
beginner
What does the
sum() function do in Python?The
sum() function adds all the numbers in an iterable (like a list or tuple) and returns the total.Click to reveal answer
beginner
How do you use
sum() with a list of numbers?You pass the list to
sum(). For example, sum([1, 2, 3]) returns 6.Click to reveal answer
intermediate
What is the purpose of the optional second argument in
sum(iterable, start)?The second argument
start sets the initial value to add to the sum. For example, sum([1, 2, 3], 10) returns 16.Click to reveal answer
beginner
Can
sum() be used with non-numeric data like strings?No,
sum() only works with numbers. Using it with strings causes an error. Use ''.join() for strings instead.Click to reveal answer
beginner
What happens if you use
sum() on an empty list?If no
start value is given, sum([]) returns 0 because it adds nothing to zero.Click to reveal answer
What will
sum([4, 5, 6]) return?✗ Incorrect
sum() adds numbers, so 4 + 5 + 6 = 15.What does
sum([1, 2, 3], 5) return?✗ Incorrect
The sum is 1 + 2 + 3 = 6, plus the start value 5, total 11.
Which data type can
sum() NOT add?✗ Incorrect
sum() only works with numbers, not strings.What is the default start value in
sum() if not provided?✗ Incorrect
The default start value is 0, so sum starts adding from zero.
What will
sum([]) return?✗ Incorrect
An empty list sums to 0 by default.
Explain how the
sum() function works and how to use its optional start value.Think about adding numbers in a list and starting from a number other than zero.
You got /3 concepts.
Describe what happens if you use
sum() on an empty list and why.Consider what adding nothing to zero results in.
You got /3 concepts.