Exceptions
Exceptions
StatisticsError
The statistics
module has its own type of exception, StatisticsError
. You may encounter this if you try to find the mean, median, or mode of an empty list.
>>> statistics.mean([])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/.../python3.10/statistics.py", line 328, in mean
raise StatisticsError('mean requires at least one data
point')
statistics.StatisticsError: mean requires at least one data
point
This is also raised if you specify less than one quantile.
>>> statistics.quantiles([3, 6, 9, 5, 1], n=0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/.../python3.10/statistics.py", line 658,
in quantiles
raise StatisticsError('n must be at least 1')
statistics.StatisticsError: n must be at least 1
StatisticsError
is actually a more specific type of ValueError
.
Exceptions when using Matplotlib
There are many different exceptions that could be raised if you make programming errors while using Matplotlib. The exception and how to fix it will depend on context. If you encounter an exception from Matplotlib, your best bet is to consult the Matplotlib documentation.
Copyright © 2023–2025 Clayton Cafiero
No generative AI was used in producing this material. This was written the old-fashioned way.