This handy #Python #One-liner can help you impress your high school cousin who is just starting to learn the calculus that you have forgotten years ago! This uses the mid-point rectangle approximation method. You can specify the number of rectangles to use in order to improve accuracy.
def integrate(function, n=1000, start=0, stop=100): return (float(stop-start)/n) * sum([eval(function) for x in [start+((i+0.5)*float(stop-start)/n) for i in range(n)]])
Use it as follows:
integrate(‘sin(x)’,1000,0,1) is equivalent to
“Never under any circumstances take a sleeping pill and a laxative on the same night.”