Run NumPy Online

Mean, Median and Standard Deviation with NumPy

Summarize a list of scores with three numbers: where the middle is and how spread out the values are.

  • Intermediate
  • 2 min read
  • 1 runnable example
What you'll learn
  • np.mean and np.median
  • np.std and what spread means
  • When the median is better than the mean
scores = [72, 81, 85, 90, 92]
mean84.0
median85.0 (the middle value)
std7.13 (typical distance from 84)

Center and spread

The problem

A list of 100 scores is hard to read. You want two quick answers: what is a typical score, and how spread out are they? The mean, median and standard deviation answer exactly those questions.

Think of it like this

Imagine students standing on a number line by score. The mean is the balance point of the line, the median is the student standing in the middle of the queue, and the standard deviation tells you how far students usually stand from the balance point.

From students on a line to code
np.mean(scores)⚖️ The balance point: 84.0
np.median(scores)🧍 The student in the middle: 85.0
np.std(scores)↔️ Typical distance from the center: 7.13
round(..., 2)✂️ Keep two decimals
Center and spread summarize a set of numbers.
In one sentence

The mean is the sum divided by the count, the median is the middle value after sorting, and the standard deviation measures how far values typically lie from the mean.

Key points

  1. np.mean(scores) adds the five scores and divides by 5: 84.0.
  2. np.median(scores) sorts them and takes the middle one: 85.0.
  3. np.std(scores) measures the spread; round(..., 2) shows 7.13.
  4. One very low score would pull the mean down much more than the median.

When should you use it?

  • Summarizing scores, prices, times and measurements.
  • Spotting unusual values that lie far from the mean.
  • Comparing two groups, such as two classes or two products.

When not to use it

  • Data with extreme outliers, such as incomes: the median describes a typical value better than the mean.
  • A sample from a larger population: use np.std(scores, ddof=1) for the sample standard deviation.

Example code

Run code →
main.py
import numpy as np

scores = np.array([72, 81, 85, 90, 92])

print("mean:", np.mean(scores))
print("median:", np.median(scores))
print("std:", round(np.std(scores), 2))
Get a Run button for this example

Result of running the code

mean: 84.0
median: 85.0
std: 7.13

Code explanation

NumPy calculates each statistic over the entire array by default. The standard deviation describes typical distance from the mean.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Change the input shape and dtype.
  2. Try negative, floating-point, and missing values.
  3. Apply the operation to larger multidimensional arrays and measure performance.
Run in the Python compiler →

Common mistakes

Using the population formula for a sample

np.std divides by n by default; a sample needs n - 1.

Problem
np.std(sample)
Fix
np.std(sample, ddof=1)

Finished this lesson?

Mark it as learned to track your progress.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges