Find elements

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.

  • 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

 

#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.

  • 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
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

  • 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
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
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

1 day ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago