Calculating Mean, Median, and Mode in Python
Introduction
When we’re trying to describe and summarize a sample of data, we probably start by finding the mean (or average), the median, and the mode of the data. These are central tendency measures and are often our first look at a dataset.
In this tutorial, we’ll learn how to find or compute the mean, the median, and the mode in Python. We’ll first code a Python function for each measure followed by using Python’s statistics
module to accomplish the same task.
With this knowledge, we’ll be able to take a quick look at our datasets and get an idea of the general tendency of data.
Table of Contents
Calculating the Mean of a Sample
If we have a sample of numeric values, then its mean or the average is the total sum of the values (or observations) divided by the number of values.
Say we have the sample [4, 8, 6, 5, 3, 2, 8, 9, 2, 5]
. We can calculate its mean by performing the operation:
(4 + 8 + 6 +