The range of a data set in Python is the difference between the maximum and minimum values.
Here's how you can calculate the range in Python:
-
Using the
max()
andmin()
functions:data = [10, 5, 20, 15, 30] range_of_data = max(data) - min(data) print(range_of_data) # Output: 25
-
Using the
numpy
library:import numpy as np data = np.array([10, 5, 20, 15, 30]) range_of_data = np.ptp(data) print(range_of_data) # Output: 25
The ptp()
function in NumPy calculates the peak-to-peak value, which is equivalent to the range of the data set.
Practical Insights:
- The range is a simple measure of dispersion, indicating the spread of the data.
- It is sensitive to outliers, meaning extreme values can significantly affect the range.
- The range is often used in conjunction with other measures of dispersion like variance and standard deviation to provide a more complete picture of the data distribution.