Python median():
With the Python statistics module, you can find the median, or middle value, of a data set. The Python median() function allows you to calculate the median of any data set without first sorting the list.
When you’re working with numbers in Python, you may want to calculate the median of a list of numbers. For instance, let’s say you’re building a program to get insights about student ages in a fourth-grade class. You may want to calculate the median age of the students.
That’s where the statistics.median()
function comes in. The statistics.median()
function is part of Python’s statistics module and can find the median of a list of values.
This tutorial will discuss how to use the statistics.median()
method. We will also walk through an example of statistics.median()
in action and will break down, line-by-line, how the method works.
This tutorial will discuss how to use the statistics.median()
method. We will also walk through an example of statistics.median()
in action and break down how the method works.
Python Median
In statistics, the median is the middle value in a sorted list of numbers. For example, for a data set with the numbers 9, 3, 6, 1, and 4, the median value is 4.
When analyzing and describing a data set, you often use median with mean, standard deviation, and other statistical calculations.
In Python, the statistics.median()
function is used to calculate the median value of a data set.
statistics.median()
is part of the statistics Python module. It includes a number of functions for statistical analysis. First, import the statistics module with this code:
import statistics
Now we can use the median()
function. Here’s the syntax for the statistics.median()
method:
import statistics
statistics.median(list_name)
The median()
method takes in one parameter: the data list.
When you call the median()
method, it will order a list of values and find its middle value. If the number of data points is odd, the middle data point will be returned. If the number is even, the median is the midpoint between the two middle values.
Python Median Example
Let’s walk through an example. Say we are building a program that to calculate all student ages in a fourth-grade class to learn about their age distribution. We want to use median()
to find out the median age of the class.
We could use this code:
import statistics student_ages = [9, 10, 9, 10, 9, 9, 10, 11, 11, 10, 9, 9] median_age = statistics.median(student_ages) print(median_age)
Our program returns:
9.5
This is the median student age.
Let’s break down our code. On the first line, we import the statistics module to access the median()
method in our code. Then, we define a list. The list is called student_ages
. It stores the ages of all the students.
Next, we use median()
and pass the student_ages
variable to calculate the median age. We assign the returned value to the variable median_age
. Then, we use print()
to print the value of median_age
to the console.
When working with median()
, the list you use should have at least one value. If you pass an empty list through the median()
method, you get this error:
StatisticsError: no median for empty data
Median of a Tuple
In addition, you can use the median method to find the median of a Python tuple. A tuple is a data type that is ordered and unchangeable. This means that it is useful when you want to store similar data that will not change over time. Tuples are declared as a list of comma-separated values enclosed within curly brackets.
Say that our data set of student ages were stored as a tuple instead of a list. If we wanted to calculate the median age of the students in the fourth-grade class, we could do so using the median()
method. Here’s the code we would use to calculate the median of our tuple:
import statistics student_ages = (9, 10, 9, 10, 9, 9, 10, 11, 11, 10, 9, 9) print(statistics.median(student_ages))
Our code returns: 9.5
.
The median value is the same, but instead of our data being stored in a list, it is stored in a tuple. This is evident because our data is enclosed within round brackets (()
) instead of square brackets ([]
).
Conclusion
You can use the statistics.median()
method to calculate the median value of a data set. Using examples, this tutorial discussed how to use the statistics.median()
method in Python to calculate the middle value of a list and a tuple.
You now have the knowledge you need to start using the statistics.median()
method like a professional Python developer!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot