×

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

How to get Time in seconds in Python

Python's time module source shows that are based on time. The time() method is used by developers and returns the current time as a UNIX timestamp. A UNIX timestamp is...

4 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.

Python program to find the GCD or HCF

Python program to find the GCD or HCF The largest positive integer that perfectly divides the two numbers is the HCF (Highest Common Factor) or GCD (Greatest Common Deviser). For example,...

5 minutes read.

Checking whether a String Contains a Set of Characters in python

In this tutorial, we will learn how to examine or check whether a string contains any set of characters or a substring and if it contains, we will learn how...

6 minutes read.

Python Project Ideas Based On Django

Introduction If you have learned Python and you are an expert in Django, then your practical skills should be excellent in this field. If you want to check your practical skills,...

9 minutes read.

Text detection using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

Difference between Module and Package in Python

The main difference between the module and the package in Python is that the module can be a simple file in Python that contains the different functions and collection of...

4 minutes read.

Anaconda python 3.7 download for windows 10 64-bit

Before installing Anaconda, you need to first know what Anaconda actually is. Anaconda is an IDE (Integrated development environment) platform used to code and executes python programs. Anaconda works both online...

3 minutes read.

Python List Size

Introduction The list data type in Python is an ordered, flexible collection. A list may also contain duplicate entries. To get the size of any object, use the len() function in...

6 minutes read.

Python md5() function

Python md5() function The md5() function in PHP calculates the md5 hash of a string. Syntax md5 ( string $str [, bool $raw_output] ) Parameter str(required)- This parameter specifies the string. raw_output(optional)- This parameter specifies a hex or binary output format: TRUE - Raw 16 character binary...

1 minute read.

Performing Transaction Using Python MySQL

Transaction A Transaction contains a set of SQL commands used to change the data in the dataset. If we say transaction, it means that the data in the database has changed....

3 minutes read.

Cx_Oracle Python with Example

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...

6 minutes read.

Python Variable Scope with Local & Non-local Examples

This article will examine Python's global, local and non-local variables and show you how to use them to write code without problems. Let's quickly review what a variable in Python is...

8 minutes read.

Python isinstance() function

Python isinstance() function The isinstance() function in Python returns a Boolean value ‘True’ if the given object is of the specified type, otherwise it returns False. Syntax isinstance(object, classinfo) Parameter object: It is a required parameter which represents an object. classinfo: This...

1 minute read.

Python reversed() Function

Python reversed() Function The reversed() in Python returns a reverse iterator. Syntax reversed(seq) Parameter seq: This parameter represents any iterable object. Return This function returns a reversed iterator object. Example 1 # Python Program describing # the reversed() function ...

1 minute read.

Python String rindex() method

Python String rindex() method The string. rindex() method in Python returns the highest index of the substring inside the string (if found). If the substring is not found, it raises an...

2 minutes read.

Python String join() method

Python String join() method The String.join() method in Python concatenates each element of an iterable (such as list, string and tuple) to the given string and returns the concatenated string. Syntax string.join(seq) Parameter Seq: This...

1 minute read.

How to import pandas in python

How to Install Pandas in Python Python is a vast ocean of libraries, modules, and different functions. It has a solution for almost everything. Using Python, we can simplify even a...

3 minutes read.

Python String isalpha() method

Python String isalpha() method The string.isalpha () method in Python returns a boolean value true if all characters in the  string are alphabetic else for any other value it returns false. Syntax isalpha()  Parameter NA Return This...

1 minute read.

Standard GUI Unit Converter using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

12 minutes read.