Finding Mean, Median, Mode in Python without Libraries

Mean, Median and Mode are very frequently used statistical functions in data analysis. Python provides built-in functions and simple algorithms to calculate these measures without external libraries.

Finding Mean

Mean of a list of numbers is also called average of the numbers. It is found by taking the sum of all the numbers and dividing it by the count of numbers. In the below example we apply the sum() function to get the sum of the numbers and the len() function to get the count of numbers ?

Example

numbers = [21, 11, 19, 3, 11, 5]

# Find sum of the numbers
num_sum = sum(numbers)

# Divide the sum by length of the list
mean = num_sum / len(numbers)

print("Numbers:", numbers)
print("Mean of the above list is:", round(mean, 2))

The output of the above code is ?

Numbers: [21, 11, 19, 3, 11, 5]
Mean of the above list is: 11.67

Finding Median

Median is the middle most value in the list of numbers. For odd count of numbers, we sort the list and choose the middle value. For even count, we take the average of the two middle values ?

Example

numbers = [21, 13, 19, 3, 11, 5]

# Sort the list
numbers.sort()

# Finding the median
if len(numbers) % 2 == 0:
    # Even number of elements
    first_median = numbers[len(numbers) // 2]
    second_median = numbers[len(numbers) // 2 - 1]
    median = (first_median + second_median) / 2
else:
    # Odd number of elements
    median = numbers[len(numbers) // 2]

print("Sorted numbers:", numbers)
print("Median of above list is:", median)

The output of the above code is ?

Sorted numbers: [3, 5, 11, 13, 19, 21]
Median of above list is: 12.0

Finding Mode

Mode is the number in the list which occurs most frequently. We calculate it by finding the frequency of each number and then choosing the one with highest frequency ?

Example

import collections

# List of elements to calculate mode
numbers = [21, 13, 19, 13, 19, 13]

print("Numbers:", numbers)

# Calculate the frequency of each item
frequency_counter = collections.Counter(numbers)
frequency_dict = dict(frequency_counter)

print("Frequency of each number:", frequency_dict)

# Find the highest frequency
max_frequency = max(list(frequency_counter.values()))
mode_values = [num for num, freq in frequency_dict.items() if freq == max_frequency]

if len(mode_values) == len(numbers):
    print("No mode in the list")
else:
    print("The Mode of the list is:", ', '.join(map(str, mode_values)))

The output of the above code is ?

Numbers: [21, 13, 19, 13, 19, 13]
Frequency of each number: {21: 1, 13: 3, 19: 2}
The Mode of the list is: 13

Alternative Mode Implementation Without Collections

Here's how to find mode using only basic Python without importing any libraries ?

numbers = [21, 13, 19, 13, 19, 13]

# Count frequencies manually
frequency_dict = {}
for num in numbers:
    if num in frequency_dict:
        frequency_dict[num] += 1
    else:
        frequency_dict[num] = 1

print("Numbers:", numbers)
print("Frequency of each number:", frequency_dict)

# Find maximum frequency
max_frequency = max(frequency_dict.values())
mode_values = [num for num, freq in frequency_dict.items() if freq == max_frequency]

print("The Mode of the list is:", ', '.join(map(str, mode_values)))

The output of the above code is ?

Numbers: [21, 13, 19, 13, 19, 13]
Frequency of each number: {21: 1, 13: 3, 19: 2}
The Mode of the list is: 13

Summary

Measure Definition Formula
Mean Average of all numbers sum(numbers) / len(numbers)
Median Middle value when sorted Middle element or average of two middle elements
Mode Most frequently occurring value Value with highest frequency count

Conclusion

These statistical measures can be easily calculated using basic Python operations. Mean uses sum() and len(), median requires sorting and indexing, while mode involves frequency counting using dictionaries or the collections.Counter module.

Updated on: 2026-03-15T17:02:58+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements