Program of Cumulative sum in python
What is Cumulative Sum?
A cumulative sum is a sequence in which each element is the sum of all of the previous elements of the original sequence plus the current element.
For example:
- Input list:
[1, 2, 3, 4] - Cumulative Sum:
- 1st Element:
1 - 2nd Element:
1 + 2 = 3 - 3rd Element:
1 + 2 + 3 = 6 - 4th Element:
1 + 2 + 3 + 4 = 10
- 1st Element:
This equals: [1, 3, 6, 10]
Program – Step by Step Explanation
Here’s the program again:
# Function to calculate the cumulative sum
def cumulative_sum(numbers):
# Create an empty list to store cumulative sums
result = []
# Initialize a variable to keep track of the running sum
running_total = 0
# Loop through each number in the input list
for number in numbers:
# Add the current number to the running total
running_total += number
# Append the running total to the result list
result.append(running_total)
# Return the cumulative sum list
return result
# Input list
numbers = [1, 2, 3, 4, 5]
# Call the function and print the result
print("Input List:", numbers)
print("Cumulative Sum:", cumulative_sum(numbers))
1. Function Definition
def cumulative_sum(numbers):
- This declares a function named
cumulative_sumthat takes in one argument:numbers: A list of numbers, such as[1, 2, 3, 4].
- This function will return the cumulative sum of the numbers in this list.
2. Initializing Variables
result = []
running_total = 0
result: This is an empty list that will be used to accumulate the sums.running_total: This is the variable in which the total of numbers that have been iterated over is stored. Its initial value should be0since no values have been added to it so far.
3. Looping Through the List
for number in numbers:
- A
forloop to iterate through each number in thenumberslist; - For every
number, the program does:- Add it to
running_total. - Append the updated
running_totalto theresultlist.
- Add it to
4. Adding to the Running Totalrunning_total += number
running_total += number
+=is short for addition.- Example:
- If
running_totalis0andnumberis1, thenrunning_totalbecomes1. - If
running_totalis 1 andnumberis2, thenrunning_totalbecomes3.
- If
5. Storing the Cumulative Sum
result.append(running_total)
- The current
running_totalis appended to theresultlist. - The
resultlist is growing after every iteration, storing the cumulative sums:- After the first number:
[1] - After the second number:
[1, 3] - After the third number:
[1, 3, 6] - And so on.
- After the first number:
6. Returning the Result
return result
- After the loop is finished, the function returns the
resultlist containing all cumulative sums.
7. Using the Function
numbers = [1, 2, 3, 4, 5]
print("Cumulative Sum:", cumulative_sum(numbers))
- This creates a list
numbersand passes it to thecumulative_sumfunction. - The function calculates the cumulative sum and prints it.
Sample Walkthrough
Input list: [1, 2, 3, 4, 5]
| Step | Current Number | Running Total | Result List |
|---|---|---|---|
| 1 | 1 | 1 | [1] |
| 2 | 2 | 3 (1 + 2) | [1, 3] |
| 3 | 3 | 6 (3 + 3) | [1, 3, 6] |
| 4 | 4 | 10 (6 + 4) | [1, 3, 6, 10] |
| 5 | 5 | 15 (10 + 5) | [1, 3, 6, 10, 15] |
Final output: [1, 3, 6, 10, 15]
Using Built-in Tools
Python’s itertools module provides a function accumulate to calculate cumulative sums:
from itertools import accumulate
# Input list
numbers = [1, 2, 3, 4, 5]
# Use accumulate to calculate the cumulative sum
cumulative_sum = list(accumulate(numbers))
print("Input List:", numbers)
print("Cumulative Sum:", cumulative_sum)
Output:
Input List: [1, 2, 3, 4, 5]
Cumulative Sum: [1, 3, 6, 10, 15]
This approach is more concise and leverages Python’s built-in tools for better readability.