Build a readable statistics report with explicit assumptions.
What It Does and Why It Matters
Start with a clean numeric dataset and state whether it represents a sample or a full population. Report central tendency (mean, median) separately from spread (minimum, maximum, standard deviation). Handle missing values before calculating; silently dropping them can skew a conclusion.
Avoid interpreting a single summary metric as the complete shape of the distribution.
Step-by-Step Example
Start with the complete example below. Read the inputs and predict what the final print statement will show before you run it.
import statistics
response_times = [100, 120, 130, 110, 250]
print(f"Count: {len(response_times)}")
print(f"Mean: {statistics.mean(response_times):.1f} ms")
print(f"Median: {statistics.median(response_times):.1f} ms")
print(f"Range: {min(response_times)}–{max(response_times)} ms")
print(f"Sample stdev: {statistics.stdev(response_times):.1f} ms")
How the Code Works
- The imports make these standard-library tools available: statistics.
- The name response_times receives the value of [100, 120, 130, 110, 250]. Changing this input lets you explore a different case.
- The print calls display the results. Compare these results with the method’s documented return type and the input used.
Expected output:
Count: 5
Mean: 142.0 ms
Median: 120.0 ms
Range: 100–250 ms
Sample stdev: 61.4 ms
Each displayed line comes from the corresponding calculation or check. The output helps verify the return values before you adapt the example to your own input.
What to Notice
Pay attention to the accepted input types, the returned value and the boundary behavior. Predict what changes if you replace the example input with an empty, zero, negative or missing value, where that value is meaningful for this operation.
If the task uses a library function, inspect its documented exceptions and return type before reusing it inside a larger function.
Where You Would Use This
Use the technique when it makes a real task easier to understand or safer to maintain. Check input and output behavior with small tests before combining it with larger application code.
Practice Exercise
Add a second dataset and explain which has greater typical delay and which has greater spread.
Before running your edited example, write down the result you expect. Then test at least one valid boundary or deliberately invalid value, using appropriate exception handling where necessary.
Reference: official Python documentation for the complete API contract and version notes.
Common Beginner Mistakes
- Mixing measurements expressed in different units or ignoring the effect of an outlier on the mean.
- Checking only the easiest input and missing a relevant error or boundary case.
Key Takeaways
- Build a readable statistics report with explicit assumptions.
- Start with a clean numeric dataset and state whether it represents a sample or a full population.
- Add a second dataset and explain which has greater typical delay and which has greater spread.