Skip to content

Question 40

Given a list L of integers, function sum_of_squares() returns the sum of the squares of each integer.
e.g., if L = [1,2,10], the sum of squares will be 1*1 + 2*2 + 10*10 = 105.

Complete the function by replacing each blank with a valid Python expression/statement.

1
2
3
4
5
def sum_of_squares(L):
    total = 0
    for i in _____1_____:
        total = _____2_____
    return total

Here is a sample run:

>>> print(sum_of_squares([2,3,10]))
113

Note that your code needs to be syntactically correct to gain marks. You cannot use any semi-colon(;) and we will deduct marks if your answer is too long.

Model Solution
1
2
3
4
5
def sum_of_squares(L):
    total = 0
    for i in L:
        total = total + i*i
    return total
  • Blank 1: L
  • Blank 2: total + i*i
    • This is also acceptable: total + i**2
    • Because the assignment operator = is already given to you, i*i to make total += i*i is not a correct answer.