In this article, we will learn How to find sum of elements in a list in Python.
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.
- Create an empty list in Python
- Takes the input from the user for the number of elements in the list
- Use a for loop to takes the elements of the list one by one
- Calculating the sum using the sum function
- Which Displays sum and average on the screen
Python Code:
1 2 3 4 5 6 7 8 9 10 |
#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)) |
Output:
1 2 3 4 5 6 7 8 9 |
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
Example 2:
1 2 3 4 5 6 7 8 9 10 |
#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)) |
Output:
1 2 3 4 5 6 7 8 9 |
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.
- Create an empty list in Python
- Declare and initialize sum variable as zero
- Takes the input from the user for the number of elements in the list.
- Use a for loop to takes the elements of the list one by one
- Calculating the sum using iterates for loop
- Which Displays sum and average on the screen
Python Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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 |
Output:
1 2 3 4 5 6 7 8 9 |
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 |
Find total using while loop- without using the sum function
- Create an empty list in Python
- Declare and initialize sum variable as zero
- Takes the input from the user for the number of elements in the list.
- Use a for loop to takes the elements of the list one by one
- Calculating the sum using iterates while loop
- Which Displays sum and average on the screen
Python Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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 |
Output:
1 2 3 4 5 6 7 8 9 |
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 |