Finding the average of a set of values is a common task in Python. In this article, we will discuss about how to find Python average of list.
For example, you may have a list of product sales and want to find out the average purchase price. Moreover, for a diversity report, you may want to calculate the average age of employees who work for your organization.
Above all, there are two methods employed to find the Python average of list of numbers:
- By calculating the sum of the list and then dividing that number by the length of the list. The length of the list is how many values are in the list.
- By using the statistics.mean() method.
Python Average: statistics.mean()
The Python statistics library contains a function called statistics.mean() that calculates the mean value of a list of values. The term mean is used in mathematics to describe the Python average of list of values.
The syntax for the statistics.mean() method is:
The mean() method takes in one parameter: the list of items whose average you want to calculate.
Before we use this method, 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.
This is an important difference between using statistics.mean() and the sum() and len() method. The sum() and len() method can be used without importing any libraries. But, you need to import statistics to use statistics.mean().
If you do not mind importing another library, this method works fine. Because you can calculate an average value without any libraries, you should consider doing so before using the statistics library.
We can import the statistics module using a Python import statement:
1 import statistics
We can now access the mean() function in the statistics library.
Now we’re ready to start calculating averages using mean().
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:
1 import statistics 2 3 orders = [2.50, 4.50, 2.00, 2.75, 2.50, 5.00, 8.00, 2.25] 4 5 average = statistics.mean(orders) 6 7 print("The average coffee order price today is $" + str(round(average, 2)))
So, let’s look at the result of our program:
1 The average coffee order price today is $3.69
Our program returns the same text and result it did in our first example.
Calculate Average of a Tuple
We can use the statistics.mean() method to calculate the mean value of a tuple, which is an unchangeable, ordered sequence of items. For instance, here’s an example of statistics.mean() being used to calculate the average coffee order price, with our information stored in a tuple:
1 import statistics 2 3orders = (2.50, 4.50, 2.00, 2.75, 2.50, 5.00, 8.00, 2.25) 4 5 average = statistics.mean(orders) 6 7 print("The average coffee order price today is $" + str(round(average, 2)), ".")
Our code returns the same response as our last example because we’re working with the same numbers:
1 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
To clarify, 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:
1 import statistics 2 3 temperatures = [-12, -14, -2, -5, -8, -4, -9] 4 5 average = statistics.mean(temperatures) 6 7 print("The average of this week's lowest daily temperatures is", round(average, 2), "degrees Celsius.")
Our code returns:
1 The average of this week's lowest daily temperatures is -7.71 degrees Celsius.
Python Average: Len() and Sum()
Most important, the formula for calculating the average of a list of values is the sum of all terms divided by the number of those terms. We can use the Python sum() and len() values to calculate the average of the numbers in a list.
To clarify, the Python len() method calculates and returns a count of numbers in a list. len is short for 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. So, we can use these functions together to calculate the average value from a list of values.
Above all, the basic formula for calculating an average in Python is:
1 avg_value = sum(list_of_values) / len(list_of_values)
This approach is common because you do not have to import any external values to calculate an average.
Sum() and len() Function Example
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 sum() and len() methods that we just discussed:
1 orders = [2.50, 4.50, 2.00, 2.75, 2.50, 5.00, 8.00, 2.25] 2 3 average = sum(orders) / len(orders) 4 5 print("The average coffee order price today is $" + str(round(average, 2)))
We print the average value to the console:
1 The average coffee order price today is $3.69
On the first line of the code, we define a Python variable. This variable stores 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.
At the end of the code, we also print out a message to the console that tells us the average price of coffee orders.
Moreover, we use the Python round() method to round our average to two decimal places. The str() method to converts the average value into a string so we can print to the console.
To sum up
You can calculate the Python average of list of values using the sum() and len() methods or the statistics.mean() method. The statistics.mean() method needs to be imported into your program. You can also use the sum() and len() method without importing any external libraries into your code.
Need to find a reliable Web/Mobile app development partner? Let’s ArrowHiTech help you!
A Web/Mobile app development company is a great choice to optimize your own websites. Above all, with high skill and amazing services, they can help you open your future dream shops. If you still have trouble contacting a Shopify developer, you can contact ARROWHITECH right now and we will provide you with the best results.
Our offered custom development services
- Ecommerce Development
- Web Application Development
- CMS Website Development
- Mobile Application Development
- Software Consulting & Development
- Graphic & Creative Design
- Testers & Developers For Hire
So, if you have any questions, don’t hesitate to contact us via:
- Email: firstname.lastname@example.org
- Phone number: +84 243 7955 813.
- Visit our product page here: MageSolution