Python program to calculate the average of numbers in a given list
In this post, you will learn how to calculate the average of numbers in a given list using the Python programming language.
A list is an arranged collection of elements. It is used to store collections of data. It can contain a list of various types of data objects with each comma separated and enclosed within a square bracket. It is mutable, which means we can change the order of elements. Individual elements can be replaced even after the list has been created. This is one of the leading reasons why lists are being widely used.
The average is defined as the mean value which is equivalent to the ratio of the sum of the number of a given set of values to the total number of values present in the set. Here, we will calculate the average of the numbers in a given list using various methods.
Average Formula = Sum of all numbers / Number of item in the list
Python average of list using for loop
In the given Python program, we are using the for loop to calculate the average of numbers in a given list. For this, we have defined a function that accepts a list as an argument. We are iterating over the list using for loop and calculating the sum. With the help of the len() method, we are finding the length or the number of elements in a list. Next, we divide the total sum of all the elements in the list by the total number of elements to find the average of the elements in the list.
# Calculate the average of numbers in a given list
# using Python program
def calculate_Average(list_x):
total_sum = 0
for i in list_x:
# calculate sum of numbers in list
total_sum = total_sum + i
# calculate average of numbers in list
avg = total_sum / len(list_x)
return avg
# Initializing list
l = [13, 23, 53, 20, 32]
# Calling function and display result
print('The average of list = %0.2f' %calculate_Average(l))
Output of the above code:
The average of list = 28.20
Python average of list using function
In the given Python program, we have used the sum() and len() functions to calculate the average of the numbers in the list. The sum() function will return the sum of all the numbers in the list, which can be divided by the total number of elements returned by the len() function.
# Calculate the average of numbers in a given list
# using Python program
def calculate_average(n):
# calculate average of numbers in list
return sum(n) / len(n)
# Initializing list
l = [33, 12, 44, 1.45, 43]
# calling function and display result
print('The average of list = %0.2f' %calculate_average(l))
Output of the above code:
The average of list = 26.69
Python average of list using mean function from statistics module
In the given program, we have used the mean function of the statistics module to calculate the average of numbers in a given list. The statistics module provides functions for calculating mathematical statistics of numeric data.
# Calculate the average of numbers in a given list
# using Python program
from statistics import mean
# Initializing list
num_list = [33, 53, 12, 59, 10, 22]
avg = mean(num_list)
print("The average of list = ", round(avg,2))
Output of the above code:
The average of list = 31.5
Python average of list using mean() from numpy library
NumPy is the fundamental package for scientific computing with Python. It is a highly optimised library for numerical operations. The support of NumPy makes the task more easier. Several libraries like OpenCV, SciPy, Matplotlib when combined with NumPy, increase the number of weapons in your arsenal. The mean() function of numpy is used to compute the average for the given list.
# Calculate the average of numbers in a given list
# using Python program
from numpy import mean
# Initializing list
num_list = [30, 10, 52, 23, 19, 54]
# Calculating average
avg = mean(num_list)
print("The average is ", round(avg,2))
Output of the above code:
The average is 31.33
Related Articles
Convert Python list to numpy arrayConvert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python for loop index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Check if list is empty Python
Python raise keyword
Python program to get the largest number from a list
Python program to map two lists into a dictionary