Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 37 ยท Getting Started

Using Python as a Statistics Calculator

A gentle Python primer for statistics: store data in lists, use sum, len, sorted and a counter, and draw your first plot, with no prior coding needed.

Statistics involves repeating the same arithmetic on many numbers. Computers are ideal for that. In this lesson you will learn the small amount of Python needed for the rest of the track. You do not need to become a programmer; you only need to read short snippets and change the numbers.

All the code runs right here in your browser. Press Run, change a number, run again.

Lists hold data

A list is an ordered collection written in square brackets. It is the natural home for a data set.

Python Lists and basic functions

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.

  • len(x) counts the values.
  • sum(x) adds them.
  • min(x) and max(x) find the extremes.
  • sorted(x) returns a new ordered list without changing the original.
  • / is ordinary division and gives a decimal.

Loops and counting

To count how often each value appears you can use collections.Counter. This is exactly what a frequency table is.

Python Count with Counter

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.

A for loop repeats a step for each item. for animal, n in counts.items(): visits each pair of name and count. The indented line is what repeats.

Formatting numbers

Statistics gives long decimals. round(x, 2) keeps two decimal places. An f-string places values in text: f"Mean = {m:.2f}" prints the value m with two decimals.

Python Rounding and f-strings

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 statistics module

Python ships with a statistics module for common summaries. Using it saves writing the formulas yourself, but in this track you will often write the formula first so you understand it, and then compare with the module.

Python Built-in statistics functions

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 last two lines give different answers on purpose. You will learn why in the lesson on standard deviation.

Your first plot

A plot makes patterns visible. The matplotlib library draws charts; plt.show() displays the result below the code.

Python A first chart

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.

Common mistakes

  • Forgetting that Python counts from 0. The first item of a list is data[0].
  • Integer division confusion. In Python 3, 7 / 2 is 3.5, while 7 // 2 is 3 (whole part only).
  • Mixing quotes and brackets. Text goes in quotes: "cat". Lists use square brackets: [1, 2, 3].
  • Changing data in place. data.sort() changes the list itself, but sorted(data) leaves it alone.

When a program stops with an error message, read the last line first. It usually names the problem, for example NameError for a misspelled variable.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does len([4, 8, 6]) return?

    len counts the values in the list. There are three.
  2. 2. Which expression gives the mean of a list called data?

    The mean is the total divided by how many values there are.
  3. 3. What is data[0] for data = [10, 20, 30]?

    Python lists start counting at 0, so the first item is at position 0.
  4. 4. What does collections.Counter do?

    Counter builds a frequency table of the values.

Summary

  • A Python list stores a data set; len, sum, min, max and sorted do the first calculations.
  • Counter builds frequency tables; f-strings and round format answers.
  • The statistics module offers mean, median, mode, stdev and more.
  • matplotlib turns lists into charts.

Next: Collecting Data: Surveys and Questions