Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 57 ยท Getting Started

Statistics with Python

Set up a Python toolkit for statistics: the built-in statistics module, NumPy arrays, random number generators with seeds, and quick summaries you can reuse all course.

Statistics is easiest to learn when you can try every formula on real numbers. This course uses Python for that, directly in your browser. You do not need to be a programmer: the code stays short, and each example prints something you can check by hand. This lesson introduces the three tools you will use constantly.

The built-in statistics module

Python ships with a statistics module for small, readable calculations on lists.

Python The statistics module

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that there are two standard deviations: stdev divides by n - 1 (sample) and pstdev divides by n (population). The lesson on variance and standard deviation explains why.

NumPy arrays

For larger data, NumPy gives fast arrays and vectorized math. Instead of looping, you apply an operation to a whole array at once.

Python NumPy basics for statistics

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Pay attention to ddof, the "delta degrees of freedom". NumPy's default std() is the population version (ddof=0), unlike the stdev of the statistics module. Mixing them up is one of the most frequent errors in beginner analyses.

Random numbers and seeds

Simulation is a powerful way to understand probability. Computers generate pseudo-random numbers from a starting value called a seed. Using the same seed reproduces the same sequence, which makes your results repeatable.

Python A reproducible random generator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Both generators printed the same dice because they started from seed 42. In real analyses, record the seed so colleagues can reproduce your numbers.

SciPy and statsmodels

When the course reaches distributions and tests, you will use SciPy (scipy.stats) for probability distributions and hypothesis tests, and occasionally statsmodels for regression tables. They are ready to use in the same editor.

Python A first look at scipy.stats

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first number is the famous 0.975 behind 95% confidence intervals; the second is 0.3125. You will derive both later.

A reusable summary function

Writing a function that prints the usual summaries saves time. You can copy this helper into later examples.

Python A one-call summary helper

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Habits for good statistical code

  1. Print intermediate numbers. If a mean looks odd, inspect the data first.
  2. Check n. Many errors come from empty lists or the wrong column.
  3. Seed random generators for reproducibility.
  4. Know the defaults. NumPy ddof=0, pandas ddof=1, Python stdev sample, pstdev population.
  5. Round only when printing, never inside a calculation chain.
flowchart LR
    A["list"] --> B["statistics module<br/>small, readable"]
    A --> C["NumPy<br/>fast arrays"]
    C --> D["SciPy<br/>distributions, tests"]
    C --> E["pandas<br/>tables"]

Choose the lightest tool that fits the job.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. np.std(x) with no extra arguments computes which standard deviation?

    NumPy's default ddof is 0, so it divides by n. Pass ddof=1 for the sample version.
  2. 2. Why set a random seed?

    The same seed gives the same pseudo-random sequence, so others can reproduce your result.
  3. 3. Which function in the statistics module gives the population standard deviation?

    pstdev uses n in the denominator; stdev uses n - 1.

Summary

  • The statistics module handles small lists; NumPy handles arrays and vectorized math.
  • ddof controls whether a standard deviation or variance is the sample or population version.
  • Seeds make random simulations reproducible.
  • SciPy provides distributions and tests for later chapters.
  • A small helper function keeps your summaries consistent.

Next: Mean, Median and Mode