To calculate the sum of
squares, do the following:
nums = [1, 2, 3, 4, 5]
s = sum(x * x for x in nums)
instead of:s = sum((x * x for x in nums)) or
s = sum([x * x for x in nums])
It introduces an extra step and creates an extra list or tuple.
Source: Python cookbook