Python provides several libraries, such as NumPy and pandas, that offer functions to calculate percentiles. Here's an example using NumPy:
import numpy as np
data = np.array([10, 15, 20, 25, 30, 35, 40, 45, 50])
# Calculate the 25th percentile
percentile_25 = np.percentile(data, 25)
print("25th percentile:", percentile_25)
# Calculate the 50th percentile (median)
median = np.median(data)
print("Median:", median)
# Calculate the 75th percentile
percentile_75 = np.percentile(data, 75)
print("75th percentile:", percentile_75)
Output:
25th percentile: 20.0
Median: 30.0
75th percentile: 40.0
In this example, we create a NumPy array called data and then use the np.percentile() function to calculate the desired percentiles.