Course 2.2Lesson 3 of 5
The statistics module describes a list of numbers. The mean is the sum divided by the count, the median is the middle value of the sorted data, or the mean of the two middle values when the count is even, and the mode is the most common value, while multimode gives all the values that tie. pstdev shows how spread out the data is. The datetime module makes a date with date of year, month and day. Subtracting two dates gives a gap, and days is the number of days. strftime writes a date as text, such as the day name, and a timedelta adds days to a date.
Try the code and the simulations beside the lesson.
After this lesson you can
The module statistics answers the questions that you ask about a list of data. There are three "averages", and they answer three different questions.
Predict first, then run it. For the data 4, 8, 6, 5, 3, 8, 9:
More points.
statistics.median([3, 9, 5, 7]) gives the mean of the two middle values (5 and 7): 6.0.mode gives the first one, and multimode gives all of them, as a list.statistics.pstdev(data) is the standard deviation: a number for how spread out the data is. Data that is close to the mean gives a small spread, and data that is far from it gives a big one.The three can be very different. A village has ten families, and nine of them earn 10 000 a month, and one earns 1 000 000. The mean is about 109 000, which describes nobody. The median is 10 000, which describes a typical family. The mean is pulled by a few very big values, and the median is not. For data with extreme values, such as income or house prices, the median is the better one. The mode is the answer to "which is the most popular?", for example the most common shoe size.
The median of five values is the third one in order. Two values above it and two below it.
The module datetime works with dates. You make a date with datetime.date(year, month, day). When you subtract two dates, you get the gap.
Predict first: on which day of the week is 11 October 2026? How many days are there until 25 December? Then run it.
(end - start).days is the number of days between two dates: 75. Subtracting dates gives a timedelta, and .days is its length in days.start.strftime("%A") writes the date as text. The code %A is the full day name (Sunday), %d is the day, %B is the full month name, and %Y is the four-digit year. The name "strftime" means "string from time".start.weekday() is a number: Monday is 0, and Sunday is 6.start + datetime.timedelta(days=100) adds 100 days and gives a new date.< and >, because they have an order.datetime.date.today() is today's date, so that output changes every day.statistics.mode of an empty list. An error. Check first.date() takes the year first, then the month, then the day. date(10, 11, 2026) is a ValueError.ValueError..days. The result is a timedelta that prints as 75 days, 0:00:00.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
statistics.mean, median and mode describe a list of numbers. pstdev shows the spread..days is the number of days.strftime writes a date as text. timedelta adds days.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
[3, 9, 5]?[1, 2, 2, 3]?[1, 2, 3, 4]?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.