;

Python Program to Find the Sum of Elements in a Given List


Tutorialsrack 04/05/2020 Python

In this Python program, we will learn how to find the sum of elements in a given list. In this program, we used the python built-in function sum() and loops to find the sum of elements in a given list.

Here is the source code of the program to find the sum of the elements in a given list.

Program 1: Python Program to find the Sum of Elements in a Given List Using sum() Function

In this program, we used the python built-in function sum() to find the sum of the elements in a given list. We pass the list as an input parameter to the sum() function to get the sum of the elements in a given list.

Python Program to find the Sum of Elements in a Given List Using sum() Function
# Python Program to find the Sum of Elements in a Given List Using sum() Function

num_list = [18, 58, 66, 85, 25, 51]

# Print the Original List
print("Original List: ", num_list)

total = sum(num_list)

print("\nThe Sum of All Element in a Given List is: ", total)
Output

Original List:  [18, 58, 66, 85, 25, 51]

 

The Sum of All Element in a Given List is:  303

Program 2: Python Program to find the Sum of Elements in a Given List Using For Loop

Python Program to find the Sum of Elements in a Given List Using For Loop
# Python Program to find the Sum of Elements in a Given List Using For Loop

total = 0
num_list = [18, 58, 66, 85, 25, 51]

# Print the Original List
print("Original List: ", num_list)

for j in range(len(num_list)):
    total = total + num_list[j]

print("\nThe Sum of All Element in a Given List is: ", total)
Output

Original List:  [18, 58, 66, 85, 25, 51]

 

The Sum of All Element in a Given List is:  303

Program 3: Python Program to find the Sum of Elements in a Given List Using While Loop

Python Program to find the Sum of Elements in a Given List Using While Loop
# Python Program to find the Sum of Elements in a Given List Using While Loop

total = 0
j=0
num_list = [18, 58, 66, 85, 25, 51]

# Print the Original List
print("Original List: ", num_list)

while(j < len(num_list)):
    total = total + num_list[j]
    j = j + 1
print("\nThe Sum of All Element in a Given List is: ", total)
Output

Original List:  [18, 58, 66, 85, 25, 51]

 

The Sum of All Element in a Given List is:  303


Related Posts



Comments

Recent Posts
Tags