0
0
Pythonprogramming~5 mins

sum() function in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AError
B456
C11
D15
What does sum([1, 2, 3], 5) return?
A11
B6
C1235
DError
Which data type can sum() NOT add?
AIntegers
BFloats
CStrings
DLists of numbers
What is the default start value in sum() if not provided?
A0
BNone
C1
DEmpty string
What will sum([]) return?
ANone
B0
CError
DEmpty list
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.