*We can find the average of a list in Python with the **sum()** and **len()** functions. Divide the **sum()** by the **len()** of a list of numbers to find the average. You can also find the average of a list using the Python **mean()** function.*

Finding the average of a set of values is a common task in Python. For example, you may have a list of product sales and want to find out the average purchase price. Or, for a diversity report, you may want to calculate the average age of employees who work for your organization.

There are a number of ways to find the average of a list in Python. In this tutorial, we will focus on the two most common approaches in Python: using `len()`

and `sum()`

, and the statistics module’s `mean()`

Python function. We will discuss how these methods work and how to use them in Python to calculate averages.

## Python Average Using Len() and Sum()

The formula for calculating the average of a list of values is the sum of all terms divided by the number of those terms. With that in mind, we can use a few built-in Python functions to calculate averages without relying on any libraries.

The Python `len()`

method calculates and returns the number of elements in a list. The term len is an abbreviation of length, referring to the length of—or number of—items in a list. The `sum()`

method in Python calculates the total sum of all values in a list. We can use these functions together to calculate the average value from a list of values.

Let’s say that we run a coffee shop and want to know the average price of all orders placed so far today. To calculate this number, we could use the following code:

orders = [2.50, 4.50, 2.00, 2.75, 2.50, 5.00, 8.00, 2.25] average = sum(orders) / len(orders) print("The average coffee order price today is $" + str(round(average, 2)))

Our code returns:

The average coffee order price today is $3.69

On the first line of our code, we define a list of order prices. Then, we calculate the average of those orders by dividing the **sum** of all orders by the **length** of our order list. Finally, we print out a message to the console that tells us the average price of coffee orders.

We use the `round()`

method to round our average to two decimal places, and `str()`

to convert our average value into a string that we can print to the console.

So, now we know the average coffee order price so far today is $3.69.

## Python Average Using Mean()

The Python statistics library contains a function called `mean()`

that we can use to efficiently calculate the mean value of a list of values. The term `mean`

is used in mathematics to describe the average of a list of values.

Instead of dividing the sum of a list of numbers by its length using built-in Python functions like we did above, we can use the statistics library to calculate the average of our list.

Before we do so, we need to import the statistics module (`statistics`

) in Python. This is a built-in module that can be used to perform various calculations in Python. We can import the module using the following code:

import statistics

Then, we can use `statistics.mean()`

to reference the `mean()`

function in the statistics library.

Now we’re ready to start calculating averages using `mean()`

. The `mean()`

method takes in one parameter: the list of items whose average you want to calculate.

Let’s take the example of coffee orders that we used above. Say we want to calculate the average price of the coffee orders placed so far today. We could use the following code to calculate the average price:

import statistics orders = [2.50, 4.50, 2.00, 2.75, 2.50, 5.00, 8.00, 2.25] average = statistics.mean(orders) print("The average coffee order price today is $" + str(round(average, 2)))

Our code returns:

The average coffee order price today is $3.69

As you can see, our program returns the same text and result it did in our first example, but instead of using `len()`

and `sum()`

, we used the `statistics.mean()`

method to calculate the average of our list of values.

In addition, we can use the `statistics.mean()`

method to calculate the mean value of a tuple, which is an unchangeable, ordered sequence of items. Here’s an example of `statistics.mean()`

being used to calculate the average coffee order price, with our information stored in a tuple:

import statistics orders = (2.50, 4.50, 2.00, 2.75, 2.50, 5.00, 8.00, 2.25) average = statistics.mean(orders) print("The average coffee order price today is $" + str(round(average, 2)), ".")

Our code returns the same response as above:

The average coffee order price today is $3.69.

A tuple may be used instead of a list if you are working with values that should not change. So, because our coffee order prices will not change after an order has been processed, a tuple is appropriate.

## Mean of Negative Values

Similarly, you can use `mean()`

to calculate the mean value of a set of negative numbers. Let’s say that we have a list that stores the lowest daily temperatures recorded during a specific week in winter. We could use the following code to calculate the average of that week’s lowest daily temperatures:

import statistics temperatures = [-12, -14, -2, -5, -8, -4, -9] average = statistics.mean(temperatures) print("The average of this week's lowest daily temperatures is", round(average, 2), "degrees Celsius.")

Our code returns:

The average of this week's lowest daily temperatures is -7.71 degrees Celsius.

## Conclusion

Calculating the average value from a data set is a common mathematical operation in Python.

This tutorial explored how you can calculate the average value from a list of values in Python using `sum()`

and `len()`

, and how you can use the `mean()`

method from the statistics module in Python to do the same. In addition, we explored examples of each of these methods of calculating averages.

Now you’re ready to start using Python calculate average functions like an expert!

**About us: **Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Read more