Python Programming Exercises,
Gently Explained
43
You can view each step of this program as it runs under a debugger at
https://invpy.com/sumproduct-
debug/.
Further Reading
Just like in Exercise #12 ―Smallest & Biggest‖ we can generate a list of one million numbers, and
then calculate the sum and product of these numbers. Multiplying one million numbers creates an
astronomically large number, so we’ll limit our test to a mere ten thousand numbers.
Write the
following program and save it in a file named
testsumproduct.py. Run it from the same folder as your
sumproduct.py file so that it can import it as a module:
import random, sumproduct
numbers = []
for i in range(10000):
numbers.append(random.randint(1, 1000000000))
print('Numbers:', numbers)
print(' Sum is', sumproduct.calculateSum(numbers))
print('Product is', sumproduct.calculateProduct(numbers))
When run, this program displays the million numbers between 1 and 1,000,000,000 it
generated,
along with the sum and product of the numbers in that list.