The easiest and the most efficient way you can calculate percentiles in Python is to make use of NumPy arrays and its functions.
Consider the following example:
import numpy as np
a = np.array([1,2,3,4,5,6,7])
p = np.percentile(a, 50)  #Returns the 50th percentile which is also the median
print(p)