SymPy Quick Start
SymPy represents mathematical expressions symbolically, so operations can preserve exact structure instead of immediately producing floating-point approximations.
from sympy import diff, integrate, simplify, solve, symbols
x = symbols("x", real=True)
expression = (x + 1) ** 2
expanded_derivative = diff(expression, x)
antiderivative = integrate(expression, x)
roots = solve(expression - 4, x)
identity = simplify((x**2 - 1) / (x - 1))
Working Rules
- Create symbols explicitly and add assumptions such as
real=Trueonly when justified. - Use
Eq(left, right)when the distinction between an equation and an expression matters. - Treat
solve()output as structured data; its shape depends on the problem and requested symbols. - Prefer a specific transformation such as
factor(),expand(), orcancel()when the desired output form is known. Generalsimplify()is heuristic and does not guarantee one canonical form.
Start with the official introductory tutorial, then consult the relevant module documentation for advanced assumptions or solvers.