Open In App

Python program to find sum of elements in list

Last Updated : 24 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will explore various method to find sum of elements in list. The simplest and quickest way to do this is by using the sum() function.

Using sum()

The sum() function is a built-in method to sum all elements in a list.

Python
a = [10, 20, 30, 40]
res = sum(a)
print(res)

Output
100

Explanation: The sum() function takes an iterable as its argument and returns the sum of its elements.

Using a Loop

To calculate the sum without using any built-in method, we can use a loop (for loop).

Python
a = [10, 20, 30, 40]

# Initialize a variable to hold the sum
res = 0

# Loop through each value in the list
for val in a:
  
    # Add the current value to the sum
    res += val

print(res)

Output
100


Next Article

Similar Reads