Python Code Example: arithmetic functions on lists

In this code, the statistics module is imported, which provides functions to perform basic statistical calculations such as mean, median, variance and standard deviation.

The myList variable is assigned a list of numbers.

The mean function calculates the average of the numbers in the list.

The median function calculates the median of the numbers in the list. The median is the middle value of a set of numbers and is a good indicator of the center of the data set. If the number of values in the list is even, the median is calculated by taking the average of the two middle values.

The variance function calculates the sample variance of the numbers in the list. The variance measures the spread of the data set and represents the average of the squared deviations from the mean.

The stdev function calculates the standard deviation of the numbers in the list. The standard deviation is the square root of the variance and is a measure of the spread of the data set.

Finally, the results of these statistical calculations are printed.

from statistics import mean, median, variance, stdev

myList = [1, 2, 3, 4, 5, 6, 7, 8]

# Return the sample arithmetic mean of data.
print(mean(myList))
# Return the median (middle value) of numeric data.
# When the number of data points is odd, return the middle data point. 
# When the number of data points is even, the median is interpolated by taking the average of the two middle values
print(median(myList))
# Return the sample variance of data.
# data should be an iterable of Real-valued numbers, with at least two values. 
# The optional argument xbar, if given, should be the mean of the data. If it is missing or None, the mean is automatically calculated.
print(variance(myList))
# Return the square root of the sample variance.
print(stdev(myList))
Output
4.5
4.5
6.0
2.449489742783178