Python Program to Find the Cumulative Sum of a List

Python’s versatility shines when it comes to solving various programming challenges. In this example, we’ll create a Python program to find the cumulative sum of a given list of numbers.

Problem Statement

Write a Python program to calculate and display the cumulative sum of a given list. Given an input list, your program should generate an output list where each element represents the cumulative sum of all preceding elements, including itself.

Python Program to Find the Cumulative Sum of a List

def cumulative_sum(input_list):
    cumulative = []
    current_sum = 0
    
    for num in input_list:
        current_sum += num
        cumulative.append(current_sum)
    
    return cumulative

# Input and Output
input_list = [3, 5, 2, 8, 10]
result = cumulative_sum(input_list)
print("Input List:", input_list)
print("Cumulative Sum List:", result)

Input / Output

Python Program to Find the Cumulative Sum of a List

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this python tutorial, you will learn how to Display Prime Numbers Between Two Intervals using the if and else...
In this python tutorial, you will learn how to Calculate Standard Deviation with built in functions of the python programming...
In this Python program, we will convert temperature values from Celsius to Fahrenheit. The Celsius and Fahrenheit scales are two...