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

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_sum that 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 be 0 since no values have been added to it so far.

3. Looping Through the List

for number in numbers:
  • A for loop to iterate through each number in the numbers list;
  • For every number, the program does:
    • Add it to running_total.
    • Append the updated running_total to the result list.

4. Adding to the Running Totalrunning_total += number

running_total += number
  • += is short for addition.
  • Example:
    • If running_total is 0 and number is 1, then running_total becomes 1.
    • If running_total is 1 and number is 2, then running_total becomes 3.

5. Storing the Cumulative Sum

result.append(running_total)
  • The current running_total is appended to the result list.
  • The result list 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.

6. Returning the Result

return result
  • After the loop is finished, the function returns the result list containing all cumulative sums.

7. Using the Function

numbers = [1, 2, 3, 4, 5]
print("Cumulative Sum:", cumulative_sum(numbers))
  • This creates a list numbers and passes it to the cumulative_sum function.
  • The function calculates the cumulative sum and prints it.

Sample Walkthrough

Input list: [1, 2, 3, 4, 5]

StepCurrent NumberRunning TotalResult List
111[1]
223 (1 + 2)[1, 3]
336 (3 + 3)[1, 3, 6]
4410 (6 + 4)[1, 3, 6, 10]
5515 (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.