# Program of Cumulative Sum in Python

### Introduction

This tutorial, explains what is meant by lists, the cumulative total, several approaches to calculating the cumulative sum of digits in a list, etc. Learn the straightforward Python codes for computing the cumulative sum of a list, which are presented with sufficient examples, and make the most of them create your own, original methods.

### Python Lists

List and Tuple are the two default container types in Python. Both classes' objects have the ability to store numerous extra items that could be accessed using an index. Sequence data types include lists and tuples, just like strings. A list or tuple can be used to store objects of various sorts.

A list is an organized grouping of items (of the same or different types), delimited by square brackets and separated by commas.

### Cumulative Sum

The total number indicates "how much so far" The total of a given sequence that rises or increases with subsequent additions is known as the cumulative sum. The increasing volume of water in a swing pool serves as a practical example of cumulative accumulation.

Find the cumulative total of the provided list in Python given a list.

### Instances of Cumulative Sum in Python

Example 1:

Input:

given list = [22, 33, 75, 3242, 10, 98, 222, 999, 34, 45, 12, 1023]

Output:

The provided list prior to computing the cumulative sum

[22, 33, 75, 3242, 10, 98, 222, 999, 34, 45, 12, 1023]

The provided list after computing the cumulative sum

[22, 55, 130, 3372, 3382, 3480, 3702, 4701, 4735, 4780, 4792, 5815]

Example 2:

Input:

given list =[53, 5465, 2453, 7678, 78, 53, 646, 75, 10, 98, 222, 45, 1023, 5, 12, 22, 33, 999, 32421, 1]

Output:

The provided list prior to computing the cumulative sum  [53, 5465, 2453, 7678, 78, 53, 646, 75, 10, 98, 222, 45, 1023, 5, 12, 22, 33, 999, 32421, 1]

The provided list after to computing the cumulative sum  [53, 5518, 7971, 15649, 15727, 15780, 16426, 16501, 16511, 16609, 16831, 16876, 17899, 17904, 17916, 17938, 17971, 18970, 51391, 51392]

### How can I Find the Total Number in a List?

There are numerous methods in Python for finding the cumulative sum, some of which include:

• Using a for loop and the count variable (Static Input)
• Using a for loop and the count variable (User Input)
• Applying Slicing (Static Input)

Go into Python Programming Samples and investigate additional examples relating to python principles to hone your programming skills in the Python programming language.

### Method #1. Using a for loop and the count variable (Static Input)

Approach:

• Provide a static input for the list.
• Create a variable to hold the sum, and set its initial value to 0.
• Consider a list that is empty and stores the cumulative sum, such as a cumulative list.
• Repeat a loop length of the provided list of times using the for a loop.
• Use the Count variable to determine the total up to the jth index.
• The append() function should be used to add this count to the cumulative list.
• Print out the cumulative list.

To determine the cumulative sum in a list, write a program.

Code:

``````# given list
given__list = [22, 33, 75, 3242, 10, 98, 222, 999, 34, 45, 12, 1023]
# Create a variable that holds the sum and set its initial value to 0.
count_sum = 0
# Consider a list that is empty and stores the cumulative sum, such as cumulative_list.
cumulative_list = []
# calculating how long a given list is
list_length = len(given__list)
# Repeat a loop length of the provided list of times using the for a loop.
for j in range(list_length):
# Using the count_sum variable, determine the sum up to the jth index.
# using the list element to increase the count
count_sum = count_sum+given__list[j]
# The append() function should be used to add this count to the cumulative_list.
cumulative_list.append(count_sum)
# prior to calculating the cumulative sum, printing the supplied list
print("The provided list prior to computing the cumulative sum ", given__list)
# After to calculating the cumulative sum, printing the supplied list
print("The provided list after computing the cumulative sum ", cumulative_list)
``````

Output:

``````The provided list prior to computing the cumulative sum  [22, 33, 75, 3242, 10,98, 222, 999, 34, 45, 12, 1023]
The provided list after to computing the cumulative sum  [22, 55, 130, 3372, 3382, 3480, 3702, 4701, 4735, 4780, 4792, 5815]
``````

### Method #2:Using a for loop and the count variable (User Input)

Approach:

• Use the split, list, and map functions to iteratively search the given input separated by spaces.
• Create a variable to hold the sum, and set its initial value to 0.
• Consider a list that is empty and stores the cumulative sum, such as a cumulative list.
• Repeat a loop length of the supplied list of times using the "for" loop.
• Use the Count variable to determine the total up to the jth index.
• The append() function should be used to add this count to the cumulative list.
• Print out the cumulative list.

The implementation is shown below:

Code:

``````# Apply the map, list, and split methods to the provided input separated by spaces.
given__list = list(map(int, input('enter a few random integers separated by spaces to the list = ').split()))
# Create a variable that holds the sum and set its initial value to 0.
count_sum = 0
# Consider a list that is empty and stores the cumulative sum, such as cumulative_list.
cumulative_list = []
# calculating how long a given list is
list_length = len (given__list)
# Repeat a loop length of the provided list of times using the for a loop.
for j in range (list_length):
# Using the count_sum variable, determine the sum up to the jth index.
# using the list element to increase the count
count_sum = count_sum + given__list[j]
# The append() function should be used to add this count to the cumulative_list.
cumulative_list.append (count_sum)
# prior to calculating the cumulative sum, printing the supplied list
print ("The provided list prior to computing the cumulative sum ", given__list)
# After to calculating the cumulative sum, printing the supplied list
print ("The provided list after computing the cumulative sum ", cumulative_list)
``````

Output:

``````enter a few random integers separated by spaces to the list = 44 23 76 3 29 789 323 454 12 45 87 465 789 4334 879 976
The provided list prior to computing the cumulative sum  [44, 23, 76, 3, 29, 789, 323, 454, 12, 45, 87, 465, 789, 4334, 879, 976]
The provided list after to computing the cumulative sum  [44, 67, 143, 146, 175, 964, 1287, 1741, 1753, 1798, 1885, 2350, 3139, 7473, 8352, 9328]
``````

### Method #3: Applying Slicing (Static Input)

Approach:

• Provide a static input for the list.
• Consider a list that is empty and stores the cumulative sum, such as a cumulative list.
• Repeat a loop length of the provided list of times using the for a loop.
• Utilize the sum and slicing functions to calculate the sum up to the jth index.
• The append() function should be used to add this count to the cumulative list.
• Print out the cumulative list.

The implementation is shown below:

Code:

``````# given list
given__list = [34, 75, 10, 98, 222, 45, 1023, 12, 22, 33, 999, 32421]
# Consider a list that is empty and stores the cumulative sum, such as cumulative_list.
cumulative_list = []
# calculating how long a given list is
list_length = len (given__list)
# Repeat a loop length of the provided list of times using the for a loop.
for j in range (list_length):
# Use slicing to calculate the sum up to the jth index.
count_sum = sum(given__list[:j+1])
# The append() function should be used to add this count to the cumulative_list.
cumulative_list.append (count_sum)
# prior to calculating the cumulative sum, printing the supplied list
print ("The provided list prior to computing the cumulative sum ", given__list)
# After to calculating the cumulative sum, printing the supplied list
print ("The provided list after computing the cumulative sum ", cumulative_list)
``````

Output:

``````The provided list prior to computing the cumulative sum  [34, 75, 10, 98, 222, 45, 1023, 12, 22, 33, 999, 32421]
The provided list after to computing the cumulative sum  [34, 109, 119, 217, 439, 484, 1507, 1519, 1541, 1574, 2573, 34994]``````