Site icon Codeforcoding

Python program to find sum of elements in a list

Python program to find sum of elements in a list.

In this tutorial, we will discuss Python program to find sum of elements in a list.

In this article, we will show you how to find the sum of numbers of the list in Python language;.

This programs take input from the user for the list of numbers and calculates sum using the list of number inputs taken.

 

Find total using sum function

find the total of integer numbers using the sum() function

Program 1

In this programs, we can see step by step approach to completion of the program.

 

#find sum of list elements
ListSum=[]#crate a empty list in Python
#takes input for number of elements in list
num=int(input("How many numbers: "));
for n in range(num):
    numbers=int(input("Enter numbers: "));
    ListSum.append(numbers)
print("Sum of numbers in list: ",sum(ListSum))

When the above code is executed, it produces the following results

How many numbers: 5
Enter numbers: 45
Enter numbers: 65
Enter numbers: 34
Enter numbers: 76
Enter numbers: 12
Sum of numbers in list: 232

 

find the total of decimal numbers using the sum() function

Program 2

#find sum of list elements
ListSum=[]#crate a empty list in Python
#takes input for total number in list
num=int(input("How many numbers: "));
for n in range(num):
    numbers=float(input("Enter numbers: "));
    ListSum.append(numbers)
print("Sum of numbers in list: ",sum(ListSum))

When the above code is executed, it produces the following results

How many numbers: 5
Enter numbers: 34.6
Enter numbers: 23.45
Enter numbers: 45.32
Enter numbers: 56.23
Enter numbers: 12.34
Sum of numbers in list: 171.94

 

Find total using forloop- without using the sum function

Program 3

In this program, we are using for loop to iterate each and every element in this list.

In this programs, we can see step by step procedure for completion of the program.

ListOfNum=[] #create a empty list
sum=0 #declare and initialize sum as zero

Number=int(input("Please enter toal number of list elements: "));
#takes input from user for number of list elements
for i in range(1, Number+1):
   count= int(input("Please enter elements in list: "));
   #takes input from user for numbers in list
   ListOfNum.append(count);

for j in range (Number):
   sum=sum+ListOfNum[j]#calculate sum

print("The totoal of all elements in list",sum)
#display result on the screen

When the above code is executed, it produces the following results

Please enter toal number of list elements: 5
Please enter elements in list: 43
Please enter elements in list: 77
Please enter elements in list: 27
Please enter elements in list: 86
Please enter elements in list: 79
The totoal of all elements in list 312

 

Program 4

Find total using while loop- without using the sum function

ListOfNum=[] #create a empty list
sum=0 #declare and initialize sum as zero
j=0;

Number=int(input("Please enter toal number of list elements: "));
#takes input from user for number of list elements
for i in range(1, Number+1):
   count= int(input("Please enter elements in list: "));
   #takes input from user for numbers in likst
   ListOfNum.append(count);

while( j<Number):
   sum=sum+ListOfNum[j]#calculate sum
   j=j+1;

print("The totoal of all elements in list",sum)
#display result on the screen

When the above code is executed, it produces the following results

Please enter toal number of list elements: 5
Please enter elements in list: 45
Please enter elements in list: 67
Please enter elements in list: 89
Please enter elements in list: 87
Please enter elements in list: 54
The totoal of all elements in list 342

 

Suggested for you

Operator in Python

For loop in Python

Python program to find largest and smallest elements in a list
Python program to find factorial of a number using while loop
Exit mobile version