import numpy as np
from scipy.integrate import trapz
Exact integral is 0.5
f = lambda x: x
a,b,n = 0.0,1.0,10
x = np.linspace(a,b,n)
y = f(x)
print("Integral = ", trapz(y,x))
Integral = 0.5
Exact integral is $1/3$
f = lambda x: x**2
a,b,n = 0.0,1.0,10
x = np.linspace(a,b,n)
y = f(x)
print("Integral = ", trapz(y,x))
Integral = 0.33539094650205764
For uniformly space points we can just pass the spacing instead of $x$.
h = (b-a)/(n-1)
print("Integral = ", trapz(y,dx=h))
Integral = 0.33539094650205753