Python Program to find Sum of Elements in a List

Write a Python Program to find Sum of Elements in a List with a practical example.

Python Program to find Sum of Elements in a List

This python program allows user to enter the length of a List. Next, we used Python For Loop to add numbers to the list.

The Python sum function returns the sum of all the elements in a List.

# Python Program to find Sum of all Elements in a List

NumList = []

Number = int(input("Please enter the Total Number of List Elements : "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

total = sum(NumList)

print("\n The Sum of All Element in this List is : ", total)
Python Program to find Sum of Elements in a List 1

Program to find Sum of Elements in a List without using sum

In this program, we are using For Loop to iterate each element in this list. Inside the loop, we are adding those elements to the total variable.

# Python Program to find Sum of all Elements in a List

NumList = []
total = 0

Number = int(input("Please enter the Total Number of List Elements : "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

for j in range(Number):
    total = total + NumList[j]

print("\n The Sum of All Element in this List is : ", total)

The sum of Python list items output

Please enter the Total Number of List Elements : 5
Please enter the Value of 1 Element : 10
Please enter the Value of 2 Element : 20
Please enter the Value of 3 Element : 30
Please enter the Value of 4 Element : 40
Please enter the Value of 5 Element : 55

 The Sum of All Element in this List is :  155

Python Program to Calculate Sum of Elements in a List using While loop

This Python program to return the sum of list items is the same as the above. We just replaced the For Loop with While loop.

# Python Program to find Sum of all Elements in a List

NumList = []
total = 0
j = 0

Number = int(input("Please enter the Total Number of List Elements : "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

while(j < Number):
    total = total + NumList[j]
    j = j + 1
print("\n The Sum of All Element in this List is : ", total)

The sum of list items using a while loop output

Please enter the Total Number of List Elements : 6
Please enter the Value of 1 Element : 10
Please enter the Value of 2 Element : 20
Please enter the Value of 3 Element : -30
Please enter the Value of 4 Element : -40
Please enter the Value of 5 Element : 50
Please enter the Value of 6 Element : 100

 The Sum of All Element in this List is :  110

Python Program to Calculate Sum of all Elements in a List using Functions

This program to find the sum of list items is the same as the first example. However, we separated the python program logic using Functions.

# Python Program to find Sum of all Elements in a List

def sum_of_list(NumList):
    total = 0
    for j in range(Number):
        total = total + NumList[j]
    return total


NumList = []
Number = int(input("Please enter the Total Number of List Elements : "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

total = sum_of_list(NumList)
print("\n The Sum of All Element in this List is : ", total)

The sum of list items using functions output

Please enter the Total Number of List Elements : 7
Please enter the Value of 1 Element : 19
Please enter the Value of 2 Element : 11
Please enter the Value of 3 Element : 32
Please enter the Value of 4 Element : 86
Please enter the Value of 5 Element : 567
Please enter the Value of 6 Element : 32
Please enter the Value of 7 Element : 9

 The Sum of All Element in this List is :  756