Search notes:

Python: sum

#!/usr/bin/python3

nums = [1, 2, 3, 4]

print(sum(nums    )) # 10
print(sum(nums,  5)) # 15
Github repository about-python, path: /built-in/functions/sum/basic.py

Sum of pairwise mulitplication

The following script uses zip to sum up the pairwise multiplication of elements of two lists.
It prints 34, which is 1*2 + 5*4 + 4*3:
x = [1, 5, 4]
y = [2, 4, 3]

p = sum(x*y for x,y in zip(x,y))

print(p)
Github repository about-python, path: /built-in/functions/sum/pairwise.py

See also

Python: Built in functions

Index