Closed-form integrals and limits are where a CAS saves the most manual work.
integrate(f, x) returns an antiderivative without the constant of integration. For a definite integral, pass a tuple: integrate(f, (x, a, b)). Limits can be infinite using oo, and multiple integrals take several tuples.
If SymPy cannot find a closed form it returns an unevaluated Integral object instead of raising an error. Always check the result type: if you see Integral(...) in the output, you can evaluate it numerically with .evalf(), which uses numerical quadrature.
Like Derivative, Integral(f, (x, a, b)) creates an unevaluated integral that .doit() computes. Results may contain special functions (erf, Si, gamma) or Piecewise expressions when the answer depends on a parameter, for example whether an exponent is -1. Declaring symbol assumptions (positive=True) often removes the Piecewise.
limit(f, x, x0) computes limits, including at oo. The direction defaults to "+" (from the right); pass dir="-" for the left, or dir="+-" for a two-sided limit, which raises an error if the one-sided limits differ. Limit(...) is the unevaluated form.
summation(f, (n, a, b)) is the discrete analogue of integrate and evaluates sums, including infinite series.
Example
from sympy import symbols, integrate, limit, Integral, oo, exp, sin, sqrt, pi, summation
x, n = symbols("x n")
print(integrate(x * exp(x), x)) # (x - 1)*exp(x)
print(integrate(exp(-x**2), (x, -oo, oo))) # sqrt(pi)
print(integrate(1 / x, (x, 1, exp(2)))) # 2
hard = integrate(sin(sin(x)), x)
print(hard) # Integral(sin(sin(x)), x)
print(Integral(sin(sin(x)), (x, 0, 1)).evalf()) # 0.430606103120691
print(limit(sin(x) / x, x, 0)) # 1
print(limit(1 / x, x, 0, dir="-")) # -oo
print(limit((1 + 1/x)**x, x, oo)) # E
print(summation(1 / n**2, (n, 1, oo))) # pi**2/6Remember
- integrate(f, x) has no +C; integrate(f, (x, a, b)) is definite
- An unevaluated Integral in the output means no closed form was found
- limit defaults to approaching from the right; use dir='-' or '+-'
Watch out for
- Assuming integrate raised an error when it actually returned an unevaluated Integral.