Summarise a set of marks using the statistics module.
What to build
A report that uses a module you have not met before.
Requirements
- Read a line of marks.
- Print
mean Mwith 2 decimals,median Dwith 1 decimal andstdev Swith 2 decimals. - Use the population standard deviation,
statistics.pstdev.
Input
One line of whole numbers separated by spaces.
Output
Three lines, as in the example.
Constraints
There are from 1 to 1000 marks, each from 0 to 100.
Example
Input:
2 4 4 4 5 5 7 9
Output:
mean 5.00
median 4.5
stdev 2.00
Why it works
The standard library already has correct, tested versions of these formulas. Finding the right tool in the documentation is a skill in its own right.
Examples
Example 1 · Input
2 4 4 4 5 5 7 9
Expected output
mean 5.00 median 4.5 stdev 2.00
Example 2 · Input
1 2 3
Expected output
mean 2.00 median 2.0 stdev 0.82
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
The statistics module has mean, median and pstdev.
Hint 2
Import it the same way as math.
Hint 3
Use format codes to show the requested number of decimals.
Solution
Show one correct solution (try it yourself first)
import statistics
marks = [int(x) for x in input().split()]
print(f"mean {statistics.mean(marks):.2f}")
print(f"median {statistics.median(marks):.1f}")
print(f"stdev {statistics.pstdev(marks):.2f}")