Skip to content
Pittu Sharma edited this page Feb 22, 2026 · 15 revisions

This page provides quick examples of common symbolic calculations in SymPy. You can print it and keep it handy for reference.

Take a look at the introductory tutorial as well. introductory tutorial.

Elementary operations

 >>> from sympy import *
 >>> x, y, z, t = symbols('x y z t')
 >>> k, m, n = symbols('k m n', integer=True)
 >>> f, g, h = symbols('f g h', cls=Function)

Construct a symbolic expression

Construct the formula $\displaystyle \frac{3 \pi}{2} + \frac{e^{I x}}{x^{2} + y}\ :$

 >>> Rational(3,2)*pi + exp(I*x) / (x**2 + y)
 3*pi/2 + exp(I*x)/(x**2 + y)

Evaluate a symbolic expression

Calculate the value of $\displaystyle e^{ix}$ for $\displaystyle x=\pi\ :$

 >>> x = Symbol('x')
 >>> exp(I*x).subs(x,pi).evalf()    #doctest: +SKIP
 -1.00000000000000

Deconstruct an expression

 >>> expr = x + 2*y
 >>> expr.__class__
 <class 'sympy.core.add.Add'>
 >>> expr.args
 (2*y, x)

Calculate a numerical value

Calculate 50 digits of $\displaystyle e^{\pi\ \sqrt{163}}\ :$

 >>> exp(pi * sqrt(163)).evalf(50)
 262537412640768743.99999999999925007259719818568888

Calculate latex representation for expression

Suppressing evaluation :

>>> latex(S('2*4+10',evaluate=False))
'2 \\cdot 4 + 10'

Allowing evaluation

>>> latex(exp(x*2)/2)
'\\frac{e^{2 x}}{2}'

Algebra

Expand products and powers

Expand $\displaystyle (x+y)^2\ (x+1)\ :$

 >>> ((x+y)**2 * (x+1)).expand()
 x**3 + 2*x**2*y + x**2 + x*y**2 + 2*x*y + y**2

Simplify a formula

Simplify $\displaystyle \left(\frac{1}{x} + \frac{x\ sin\ x - 1}{x} \right)\ :$

 >>> a = 1/x + (x*sin(x) - 1)/x
 >>> simplify(a)
 sin(x)

Solve a polynomial equation

Find the roots of $\displaystyle x^3 + 2x^2 + 4x + 8\ :$

 >>> solve(Eq(x**3 + 2*x**2 + 4*x + 8, 0), x)
 [-2*I, 2*I, -2]

or more easily:

>>> solve(x**3 + 2*x**2 + 4*x + 8, x)
 [-2*I, 2*I, -2]

For details, see: Finding roots of polynomials.

Solve an equation system

Solve the equation system $\displaystyle \{\ x + 5y = 2,\ -3x + 6y = 15 \}\ :$

 >>> solve([Eq(x + 5*y, 2), Eq(-3*x + 6*y, 15)], [x, y])
 {x: -3, y: 1}

or

 >>> solve([x + 5*y - 2, -3*x + 6*y - 15], [x, y])
 {x: -3, y: 1}

Solve a recurrence relation

Solve $\displaystyle y_0 = 1, \quad y_1 = 4, \quad y_n = 2y_{ n-1 }+5{ y }_{ n-2 }\ :$

>>> y=Function('y')
>>> n=Symbol('n', integer=True)
>>> f=y(n)-2*y(n-1)-5*y(n-2)
>>> rsolve(f,y(n),[1,4])
(1/2 - sqrt(6)/4)*(1 - sqrt(6))**n + (1/2 + sqrt(6)/4)*(1 + sqrt(6))**n

Calculate a sum

Evaluate $\displaystyle \sum_{n=a}^b 6 n^2 + 2^n\ :$

 >>> a, b = symbols('a b')
 >>> s = Sum(6*n**2 + 2**n, (n, a, b))
 >>> s
 Sum(2**n + 6*n**2, (n, a, b))
 >>> s.doit()
 -2**a + 2**(b + 1) - 2*a**3 + 3*a**2 - a + 2*b**3 + 3*b**2 + b

Calculate a product

Evaluate $\displaystyle \prod_{n=1}^b n(n+1)\ :$

 >>> product(n*(n+1), (n, 1, b))
 RisingFactorial(2, b)*b!

Solve a functional equation

Example: if $\displaystyle \ f \left(\frac{1}{x} \right) - 3f(x) = x$ then find $\displaystyle f\left(2f(x) \right)\ :$

>>> f=Function('f')
>>> ex=Eq(f(1/x)-3*f(x),x)
>>> ex.subs(x,2)
f(1/2) - 3*f(2) == 2
>>> ex.subs(x,Rational(1,2))
-3*f(1/2) + f(2) == 1/2
>>> solve([f(Rational(1,2))-3*f(2)-2,-3*f(Rational(1,2))+f(2)-Rational(1,2)])
[{f(2): -13/16, f(1/2): -7/16}]

Calculus

Calculate a limit

Evaluate $\displaystyle \lim_{x\to 0} \frac{\sin\ x - x}{x^3}\ :$

 >>> limit((sin(x)-x)/x**3, x, 0)
 -1/6

Calculate a Taylor series

Find the Maclaurin series of $\displaystyle \frac{1}{\cos\ x}$ up to the $\displaystyle O(x^6)$ term:

 >>> (1/cos(x)).series(x, 0, 6)
 1 + x**2/2 + 5*x**4/24 + O(x**6)

Calculate a derivative

Differentiate $\displaystyle \frac{\cos(x^2)^2}{1+x}\ :$

 >>> diff(cos(x**2)**2 / (1+x), x)
 -4*x*sin(x**2)*cos(x**2)/(x + 1) - cos(x**2)**2/(x + 1)**2

Calculate an integral

Calculate the indefinite integral $\displaystyle \int\ x^2 \cos\ x\ dx\ :$

 >>> integrate(x**2 * cos(x), x)
 x**2*sin(x) + 2*x*cos(x) - 2*sin(x)

Calculate the definite integral $\displaystyle \int_0^{\pi/2} x^2 \cos\ x\ dx\ :$

 >>> integrate(x**2 * cos(x), (x, 0, pi/2))
 -2 + pi**2/4

Solve an ordinary differential equation

Solve $\displaystyle f''(x) + 9 f(x) = 1\ :$

 >>> f = Function('f')
 >>> dsolve(Eq(Derivative(f(x),x,x) + 9*f(x), 1), f(x))
 f(x) == C1*cos(3*x) + C2*sin(3*x) + 1/9

You can also use .diff(), like here (an example in isympy)

 >>> f = Function("f")
 >>> Eq(f(x).diff(x, x) + 9*f(x), 1)
 9*f(x) + Derivative(f(x), x, x) == 1
 >>> dsolve(_, f(x))
 f(x) == C1*cos(3*x) + C2*sin(3*x) + 1/9

Clone this wiki locally