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.
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
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
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
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
For loop in Python
Python code to Calculate sum of odd and even in a list In this tutorial,…
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…