One of the best things about Python is the language’s sheer flexibility. We have a wide range of elements and containers to work with. And we can often manipulate those items in a wide variety of useful ways. For example, we can find the cumulative sum of a python list despite the fact that it’s not an integer of any type. Check out the small Python program below to see a simple example of how we might find the sum of list Python programming style.
numList = [-90,10, 15, 20, 25, 30,35.5] ourSum = sum(numList) print(ourSum) ourSum = sum(numList, 10) print(ourSum)
We begin by creating a sum list of numbers, with each given list element corresponding to a single natural number. In other languages we’d typically want to construct a loop to work with each of the given list’s numbers. But Python provides us with a specific Python function that relieves us of that burden, once we put in the start value it will add the other numeric values. We can instead use the python sum function.
In line 2 we pass the numList variable as a parameter to the sum function. We can then print out the total of our new ourSum variable to see the total cumulative sum of the Python list. In the next line we repeat this process but pass both numList and an integer as parameters to the inbuilt function sum. We then print it out to demonstrate another interesting facet of element wise sum. If we pass a natural number to sum it will use that as the start value. If we don’t pass any initial value numbers to sum the Python function defaults to a value of 0.
Note too that the Python sum is able to handle different types of numbers rather than insisting on one type shared among every single value. For example, it can work with the -90 and 35.5 without needing to perform any conversions or specify type on the given number. One of the few things that we couldn’t handle by default with the inbuild function sum is a string value. That doesn’t mean we can’t find a way around that data structure problem though. Consider the following Python code.
ourList = ['1', 2, '3', '4', '5'] ourList = [int(i) for i in ourList] print(sum(ourList))
The ourList variable consists of a list made up of numbers ranging from one to five. However, all the numbers except for 2 are actually strings. Sum would normally exit the Python interpreter with an error if we tried to pass ourList to it. But we can use list comprehension to modify the contents of ourList before passing it to sum. We redefine ourList with the output of a generation loop consisting of the items in the original version of ourList.
Each item is passed to the int function as input to convert it into an integer. The 2, which is already an integer, remains unchanged. The final line prints what we see after passing the modified ourList to sum. In short, we were able to use list comprehension to convert a mixed content original list of strings and integers into a list of integers. The converted list was then easily parsed by sum.
The combination of sum and list comprehension might spur some related ideas. For example, could we combine sum and a generator expression? We can, and the following example shows how powerful this combination can be.
ourSum = sum(z ** 2 for z in range(5)) print("generator sum: ", ourSum)
We begin by using a generator expression as a parameter to pass to sum variable. The expression sums the values immediately on generation without needing to save or convert anything in memory. This makes it extremely efficient for longer or more complex mathematical functions. Though in this instance we’re simply finding the sum of the squares from 0 to 5. If we encased the parameters passed to sum variable with brackets than a Python program would go through the full process of creating and saving a list before converting it to a sum.