Average() Function in Python

Someone recently asked how to compute averages in Python. In this tutorial, I will explain how to use the average function in Python using various methods for calculating averages with practical examples.

To calculate the average in Python using basic arithmetic operations, you can simply sum all the numbers in your dataset and then divide by the count of numbers. For example, given a list numbers = [23, 45, 67, 89, 12, 34, 56], you can calculate the average with average = sum(numbers) / len(numbers).

What is the Average Function in Python?

The average (or mean) is a measure of central tendency that is commonly used to summarize a set of numbers. It is calculated by adding all the numbers in a dataset and then dividing by the count of numbers. In Python, there isn’t a built-in average function, but you can easily calculate it using basic arithmetic operations or by leveraging libraries like statistics and numpy.

Syntax of Python Average Function

To calculate the average in Python, you can use the following approaches:

  1. Using Basic Arithmetic Operations
  2. Using the statistics Module
  3. Using the numpy Library

Using Basic Arithmetic Operations

The simplest way to calculate the average is by using basic arithmetic operations. Here’s how you can do it:

# List of numbers
numbers = [23, 45, 67, 89, 12, 34, 56]

# Calculate the average
average = sum(numbers) / len(numbers)

print("The average is:", average)

In this example, we first sum all the numbers in the list using the sum() function and then divide by the length of the list using the len() function.

You can see the exact output in the screenshot below:

Python Average Function

Check out Python Function Return Types

Using the statistics Module

Python’s statistics module provides a built-in function to calculate the mean, making it more convenient and readable.

import statistics

# List of numbers
numbers = [23, 45, 67, 89, 12, 34, 56]

# Calculate the average
average = statistics.mean(numbers)

print("The average is:", average)

The statistics.mean() function takes a list of numbers and returns their average.

You can see the output in the screenshot below:

average function in python

Using the numpy Library

For more advanced numerical operations, the numpy library is a powerful tool. It provides a mean function to calculate the average in Python.

import numpy as np

# List of numbers
numbers = [23, 45, 67, 89, 12, 34, 56]

# Convert the list to a numpy array
array = np.array(numbers)

# Calculate the average
average = np.mean(array)

print("The average is:", average)

Using numpy is particularly useful when working with large datasets or performing complex numerical computations.

Check out Python Function Within Function

Average Function in Python with Examples

Example 1: Average Age of Employees

Imagine you have a list of ages of employees in a company based in New York, and you want to calculate the average age.

import statistics

# List of ages
ages = [25, 30, 45, 50, 29, 34, 40]

# Calculate the average age
average_age = statistics.mean(ages)

print("The average age of employees is:", average_age)

You can see the output in the screenshot below:

Average Function in Python with Examples

Example 2: Average Temperature in a Week

Let’s say you are analyzing the average temperature in Los Angeles over a week.

import numpy as np

# List of temperatures
temperatures = [70, 75, 68, 72, 74, 73, 71]

# Convert the list to a numpy array
temp_array = np.array(temperatures)

# Calculate the average temperature
average_temp = np.mean(temp_array)

print("The average temperature in Los Angeles over the week is:", average_temp)

Read MAX Function in Python

Example 3: Average Test Scores

Consider a scenario where you have the test scores of students in a San Francisco school, and you want to find the average score.

# List of test scores
test_scores = [88, 92, 79, 85, 90, 91, 87]

# Calculate the average test score
average_score = sum(test_scores) / len(test_scores)

print("The average test score is:", average_score)

You can see the output in the screenshot below:

python average function example

Conclusion

In this tutorial, I explained how to calculate the average in Python using multiple ways like using basic arithmetic operations, the statistics module, or the numpy library, etc.

I hope this tutorial helped you understand how to calculate averages in Python. If you still have questions, feel free to leave a comment below.

You may also like:

Leave a Comment