Find elements

Python program to find Average of numbers in a list

Python program to find Average of numbers in a list

In this tutorial, we will discuss a concept of Python program to find Average of numbers in a list.

This program uses to calculate the sum and average of the integer number in the list of Python language.

If the list of numbers given to find the average, the sum of the numbers divided by the number of elements of the list.

Program 1

Calculate average in an integer number list

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

  • Define the sum variable and initialize as zero
  • Create a number list in Python language
  • use a for loop for calculating the sum
  • then, calculates the average of the list elements
  • Which Displays sum and average on the screen

 

sum=0
#find sum of list elements
ListSum=[45,78,54,23,67]#crate a list in Python
for num in ListSum:
      sum=sum+num;#calculate sum

      avg=sum/len(ListSum)#calculate average
print("Sum of list elements is ",sum)
print("Average of list elements is ",avg)
#display result on the screen

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

Sum of list elements is 267
Average of list elements is 53

 

Program 2

Calculate average in a decimal number list

sum=0
#find sum of list elements
ListSum=[45.45,78.23,58.54,96.23,45.67]#crate a list in Python
for num in ListSum:
      sum=sum+num;            #calculate sum

      avg=sum/len(ListSum)    #calculate average
print("Sum of list elements is ",sum)
print("Average of list elements is ",avg)
#display result on the screen

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

Sum of list elements is 324.12
Average of list elements is 64.824

 

Program 3

Calculate average in an integer number list -takes input from the user

This program takes input from the user one by one and calculates sum and average of the integer number in the list

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

  • Define the sum variable and initialize as zero
  • Create an empty list in Python language
  • Takes input from the user for the number of elements in the list
  • Use a for loop to store element in the list and calculate the sum
  • Then, calculates the average of the list elements
  • Finally, Which is Displayed sum and average on the screen
sum=0
#find sum of list elements
ListSum=[]          #crate a empty list in Python
#takes input for numbers in list
num=int(input("how many numbers: "))
for n in range(num):
    numbers=int(input("Enter numbers: "))
    sum=sum+numbers;
avg=sum/num
print("Sum of list elements is ",sum)
print("Average of list elements is ",avg)

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

how many numbers: 5
Enter numbers: 12
Enter numbers: 23
Enter numbers: 34
Enter numbers: 45
Enter numbers: 56
Sum of list elements is 170
Average of list elements is 34)

 

Program 4

Calculate average in a decimal  number list -takes input from the user

This program takes input from the user one by one and calculates sum and average of the decimal number in the list

sum=0
#find sum of list elements
ListSum=[]#crate a empty list in Python
#takes input for numbers in list
num=int(input("how many numbers: "))
for n in range(num):
    numbers=float(input("Enter numbers: "))
    sum=sum+numbers;
avg=sum/num
print("Sum of list elements is ",sum)
print("Average of list elements is ",avg)

 

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

how many numbers: 6
Enter numbers: 12.34
Enter numbers: 23.45
Enter numbers: 34.56
Enter numbers: 45.67
Enter numbers: 56.78
Enter numbers: 67.89
('Sum of list elements is ', 240.69)
('Average of list elements is ', 40.115)

 

Suggested for you

for loop in Python

operator in Python

Python program to find largest number among three numbers
Python program to find largest and smallest elements in a list
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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago