Calculating Averages in Python

When dealing with data analysis, understanding the central tendency of a dataset is crucial for gaining meaningful insights. Python provides straightforward methods to compute different types of averages—mean, mode, and median. Let’s delve into each of these measures and provide code samples for their computation.

The Mean

The mean, often referred to as the average, is computed by summing up all the values in a dataset and then dividing by the total number of values. The mean is appropriate for cases where you want to find the typical value that represents the overall trend.

Example: Calculating the average score of students in a class

def calculate_mean(data):
    total = sum(data)
    count = len(data)
    mean = total / count
    return mean

scores = [85, 90, 78, 92, 88, 95, 70, 80]
mean_score = calculate_mean(scores)
print("Mean Score:", mean_score)

The Mode

The mode represents the value that appears most frequently in a dataset. It’s useful when you want to identify the most common value or category within the data.

Example: Identifying the most frequently purchased item in a store

Here we are using the statistics module to help calculate the mode. It might be an interesting exercise to implement this for yourself.

from statistics import mode

items_sold = ["apple", "banana", "apple", "orange", "banana", "apple", "apple", "banana"]
most_common_item = mode(items_sold)
print("Most Common Item:", most_common_item)

The Median

The median is the middle value of a dataset when arranged in ascending order. It’s suitable for scenarios where you want to find a value that isn’t affected by extreme values, making it a robust measure for skewed distributions.

Example: Determining the median income of a population

Again, we are using an external module. If you would like a challenge, think about how you would calculate the median without an external library and have a go at implementing your idea.

from statistics import median

incomes = [25000, 30000, 35000, 40000, 45000, 50000, 60000, 100000]
median_income = median(incomes)
print("Median Income:", median_income)


In this post, we explored how to calculate different types of averages—mean, mode, and median—using Python. Each type of average is appropriate for specific scenarios, depending on the nature of the data and the insights you’re seeking. By leveraging these measures, you can gain a deeper understanding of your data and make informed decisions based on its characteristics.

Sharing is caring!

Leave a Reply

Your email address will not be published. Required fields are marked *