Python Average is a function that is used to find the average of a given set of numbers.

One of the most widely used functions is the mean() function. The mean() is a built-in module function used to calculate the average of numbers and lists. The mean() returns the mean of the data set passed as parameters.

There are also other ways to find out the average in python such as using the loop, by using sum() and len() built-in average function in Python.

How to Find Average in Python using Loop?

In this, let’s find the average in python using a for loop.

For example,

def calculate_average(num):
    sum_num = 0
    for t in num:
        sum_num = sum_num + t

    avg = sum_num / len(num)
    return avg


print("The average is", calculate_average([18, 25, 3, 41, 5]))

As a result, the output is:
The average is 18.4

Here, we have initialized the variable sum_num to zero and used a for loop. The for-loop loops through the elements present in the list.

Then each number is added and saved inside the sum_num variable.

The average is calculated by using the sum_num and dividing it by the number of elements. The len() function is also used in this case.

Using len() and sum() functions to find Average in Python

Python sum() is a built-in function that returns the sum of all list elements. The len() function gives the number of items in the list. Using the combination of these two inbuilt functions, we find out the average of a list in python.

For example,

def averageOfList(numOfList):
    avg = sum(numOfList) / len(numOfList)
    return avg


print("The average of List is", round(averageOfList([19, 21, 46, 11, 18]), 2))

As a result, the output is:
The average of List is 23.0

Using mean function from numpy Library

There is a NumPy.mean() function that returns the average of the array elements.

The Numpy library is a commonly used library to work on large multi-dimensional arrays. It also contains a huge collection of mathematical functions to be used on arrays to perform various tasks.

For example,

from numpy import mean
number_list = [19, 21, 46, 11, 18]
avg = mean(number_list)
print("The average of the given list is ", round(avg, 2))

As a result, the output is:
The average of the given list is 23.0

By using the statistics.mean() function

The Py statistics library contains a function called statistics.mean() that helps us to calculate the mean value of a given list of values.

The syntax for the statistics.mean() method is:
statistics.mean(list_of_values)

Note: Before we use this method, we need to import the statistics module in Python. This is a built-in module that can be used to perform various calculations in Python.

For example,

import statistics

orders = [250, 450, 200, 275, 250, 500, 800, 225]

average = statistics.mean(orders)

print("The average coffee order price today is Rs" + str(round(average, 2)))

As a result, the output is:
The average coffee order price today is Rs368.75

Calculating the Average of a Tuple in Python

To calculate the average of a tuple we use statistics.mean() method in the same way as mentioned earlier.
For example,

import statistics

tupleA = (1, 9, 2, 1, 1, 8)
print(statistics.mean(tupleA))

As a result, the output is:
3.6666666666666665

Here, we take a tuple containing different values and then apply the statistics.mean() method to take out the average value of the given tuple in python.

Calculating Average of Negative values in Python

We can use mean() to calculate the average value of a set of negative numbers.
For example,

import statistics

temperatures = [-12, -14, -2, -5, -8, -4, -9]

average = statistics.mean(temperatures)

print("The average of the temperatures is", round(average, 2), "degrees Celsius.")

As a result, the output is:
The average of the temperatures is -7.71 degrees Celsius.

Conclusion

In this article, we have learnt how to find averages using different ways as shown above. There are also different packages that are needed to be imported into the program. Finding average is a pretty easy task and can be performed without much hassle. To find out more about such exciting python stuff, stay in touch with the coding community.

You can also learn about Python Frozenset.

Like this article? Follow us on Facebook and LinkedIn.