×

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]

Related Topics

Python Sys Stdout

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Python Set clear() Method

Python Set clear() Method The set.clear() method removes all the elements from the set. Syntax set.clear() Parameter NA Return None Example 1 # Python program explaining # the set.clear() method # initializing the set fruits = {"watermelon", "banana", "apple"} # printing the set...

2 minutes read.

How to reverse a string in Python

In Python language, we have a few ways to reverse a string. In this article, let us discuss these ways and understand the suitability of these ways in different scenarios...

5 minutes read.

Nested List in Python

The list is an inbuilt sequential data type in Python. It is a very useful and frequently used data type. A list can store any number of data items of...

4 minutes read.

How to Configure Python Interpreter in Eclipse

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

PyShark in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

Installing Packages in Python

It's critical to understand that the term "package" here refers to a collection of software that must be installed (i.e. as a synonym for a distribution). It has nothing to...

6 minutes read.

Python String zfill() method

Python String zfill() method The string.zfill() method in Python returns a copy of the string while adding the zeros (0) at the beginning of the string, until it reaches the specified...

1 minute read.

Python String strip() method

Python String strip() method The string. strip() method in Python removes any leading (spaces at the beginning) and trailing (spaces at the end) characters (space is the default leading character to...

1 minute read.

Check Letter in a String Python

Checking a specific letter or a character in a given string or a string which is taken as an input from the user is possible in Python in broadly three...

3 minutes read.

XXhash Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

6 minutes read.

How to print in same line in python

How to Print in the Same Line in Python To print in Python, we use the print () function and the syntax of print () goes like this: print (values, sep =...

3 minutes read.

What is online python free IDE

An IDE is an acronym that stands for “Integrated development environment.” It is a software environment that has a software development package that consists of the code editor and builds...

4 minutes read.

How to print in the same line in Python?

How to print in the same line in python By default, the print function in Python takes us to the next line and prints the desired statement in the output. In this...

5 minutes read.

What is Ipython shell?

IPython is a command shell for computing in multiple programming languages. IPython was developed for python language by Fernando Perez in 2001 as a well-equipped python interpreter that offers shell...

3 minutes read.

Comments in the Python Programming Language

In this tutorial, we will learn how to comment in multiple lines and paragraphs in the python programming language. Commenting a Paragraph in Python Most people think the importance of comments is...

2 minutes read.

Data Class in Python

Introduction In Python 3.7, the dataclass modules are introduced as a practical tool for creating organized classes designed specifically for data storage. These classes contain specific attributes and capabilities to deal...

6 minutes read.

Python List pop() method

Python List pop() method The list.pop() method removes the item at the specified position in the list, and return it. If no index is specified, this method removes and returns the last item in the list. Syntax list.pop([i]) Parameter i:...

1 minute read.

Selection Sort Using Python

Selection sort is a type of sorting algorithm which is based on sorting elements in increasing order or ascending order through comparison. This sorting technique does not take extra space...

3 minutes read.

Falcon Python

Introduction of Python Python is the fastest and the smartest programming language and it is an object oriented language. Python has libraries that can be importedeasily and perform many operations; to...

3 minutes read.