TØ 4. Extra

This note contains additional exercises related to laboratory exercise 1.

1 Mean and standard deviation

For the pipette exercise we implemented a function to calcualte the mean of three values.

The exercise belows explores how to create a more general function that calculates the mean of any number of values.

1.1 Exercise: General mean function

A more general equation for the mean is

\[ m(X) = \frac{1}{|X|} \sum_{x \in X} x \]

Where \(X\) is a set of numbers, \(|X|\) is the size of the set i.e. the amount of numbers. The function we implemented before would only take the average of three numbers, while that is useful it would be better to have a more general function.

To build this function we will need a few changes

  1. We need the input to the function to be an arbitrary number of numbers.
  2. We need to sum over all the input numbers.
  3. We need to divide by the number of inputs numbers.

Luckily, all of this is very achievable in Python.

  1. We will use a list of numbers [1, 2, 3, 4, ...] as input.
  2. We will use the sum-function to calculate the sum
  3. We will use the len-function to find the length of the input list.

Finish the code in the cell below