×

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 Program for Tower of Hanoi

In this tutorial, we will examine and learn about the Python coding used to create the video game Tower of Hanoi. Before seeing the game's step-by-step implementation in Python, we...

3 minutes read.

YOLO Python

YOLO means you only look once.It is a technique to perform object detection is called YOLO. It is the algorithmused by the program to identify items in the image. Earlier detection...

4 minutes read.

Difference between Mutable and Immutable Objects

Difference between Mutable and Immutable Objects As we all know that Python is an object-oriented programming language. Object-oriented programming approach is based on the objects and the classes’ stores the data...

4 minutes read.

Defaultdict in Python

In this tutorial, we will study What is defaultdict in Python We will understand it with the aid of certain examples. Before this, we will have a look at dictionaries...

3 minutes read.

Python Syntax

Python is a strong object-oriented programming language that is simple to learn. Python was created to be a very readable programming language. The syntax of the Python programming language is...

8 minutes read.

Python Functionalities of Pillow Module

This instructional exercise is about "Pillow" library, one of the significant libraries of python for picture control. Pillow library of python, is an easy to use and also open source...

14 minutes read.

Parameter Passing in Python

In Python, when we characterize capabilities with default values for specific boundaries, it is said to have its contentions set as a possibility for the client. Clients can either pass their...

3 minutes read.

Python for Loop Increment

Introduction In general, loops are employed for sequential traversal. It belongs to the definite iteration category. Definite iterations imply that the number of iterations is explicitly set in advance.  In this article,...

4 minutes read.

os.rename() method in Python

In Python, the os module provides the capacity to interact with the operating system. The operating system comes under the Python module. In this module, Python provides a specific feature...

3 minutes read.

Permutations in Python

Recursion Basic idea: for numbers of length N. N-1 items are chosen at random between 0 and then generate permutations using the remaining N-1 elements in a recursive fashion. Once you've done...

4 minutes read.

How to Pass a list as an Argument in Python

Lists in Python A list is a datatype in python which is used to store the data in a sequence. The lists are mutable; that is, we can change the values...

3 minutes read.

Python Web Development projects

What is Python? Python is currently one of the most widely used computer programming languages. This isn't just an exaggeration; Python is the second-most famous programming language on the software development...

3 minutes read.

Write Text files in Python

Let's discuss writing the text file in detail. Generally, writing text also follows the same procedure as like reading text. It varies only in some specific places. Steps followed for writing...

4 minutes read.

Python setattr() function

Python setattr() function The setattr() function sets the value of the specified attribute of the specified object. Syntax setattr(object, name, value) Parameter object: This parameter represents any object. name: This parameter represents the name of the attribute you...

1 minute read.

Python Time Module

Python contains many files that can be imported into a python code and used whenever we want. One of that modules is the time module. It is a good practice...

6 minutes read.

Python String count() method

Python String count() method The string.count() method returns the number of times a specified value appears in the string. Syntax string.count(sub[, start[, end]]) Parameter sub: This parameter represents a substring to be searched. start: This parameter...

1 minute read.

Python Function

Python Function A Python function is a reusable, organized block of code that is used to perform the specific task. The functions are the appropriate way to divide an extensive program into a...

7 minutes read.

Not supported between instances of str and int in python

In this article, you are going to learn why the type error occurs between instances of string and integer datatypes. Also, you will know what kind of error is the...

6 minutes read.

Import py file in Python

In Python programming language, a module is a single layer of block of Python code that can be loaded and used by importing into other Python block of code. A module...

4 minutes read.

_name_ in Python

Introduction: The code at level 0 indentation is to be performed when the command to run a Python program is supplied to the interpreter because Python does not have a main() function...

4 minutes read.