Python SymPy Tutorial – Symbolic Computation Library

SymPy is a free open source library in Python, used for Symbolic and Mathematical Computation. In this tutorial, we will cover how to effectively use the Python SymPy Library to perform mathematical operations on expressions, Symbolic computations and other various algebraic properties.

Symbolic Computation with SymPy

SymPy allows us represent data and values in a different manner, that is 100% accurate. Let’s take a look at the following example.

import math


What this does, is returns an approximation of the sqaure root of 7. It’s not a 100% exact answer. The more decimal places you include, the more accurate the answer gets. So technically this will never be an exact representation.

Now let’s try the same thing, but with SymPy instead.

import sympy


What SymPy returned here, was not a mathematical value, but rather a symbolic representation of the sqrt of 7. Thus, it can never be inaccurate. We can use this symbolic representation in other calculations, with a higher degree of accuracy.

Here’s an another interesting snippet.

import sympy
import math

print(math.pow(math.sqrt(7), 2))
print(sympy.sqrt(7) ** 2)

We took the sqrt root of 7 with both libraries, then applied a power of 2 on both. SymPy returned the correct value, whereas the math library didn’t (because it works off approximations).

Other interesting things SymPy can do, is return simplified representations.


The next thing to cover in this Tutorial, is how to create and manipulate expressions in Python SymPy.

Mathematical Expressions with SymPy

SymPy gives us the ability to create actual expressions using variables (called symbols in SymPy). For example, if you want to represent the equation 2x2 + 4x + 5 in Python, how would you do so? You could represent such an equation in a string, but it would be of little use without SymPy.

SymPy gives us the ability to create and handle such expressions that can actually be used in computation. Python SymPy has various functions which could perform actual operations such as differentiation and integration, and return the resultant expression! (We’ll take a look at these later in the tutorial)

So how do we create such an expression? Well it’s simple. First we define a symbol, which represents an unknown/variable like “x” or “y”.

from sympy import symbols

x, y = symbols("x y")

We can now use x and y to construct expressions. No need to make a string or anything, just do it normally. Let’s try out a few examples.

from sympy import symbols
x, y = symbols("x y")

expr1 = 2*x + 4*y       # 2x + 2y
expr2 = 2*(x**2) + 5   # 2(x^2) + 5
expr3 = x**2 + y**2    # x^2 + y^2

Modifying SymPy Expressions

We can even do cool stuff like modify these expressions, by adding, subtracting or multiplying constants and other symbols from it.

expr1 += 5
2*x + 4*y
2*x + 4*y + 5

SymPy will even automatically adjust the expressions as you modify it. For example, if you have an expression "2x", and you add "x" to it, you might expect it to just concatenate and become 2x + x. But nope, SymPy will automatically simplify it to 3x.

Here’s a short code snippet showing this.

expr = 2*y + x + 5
expr -= x
expr += 3
x + 2*y + 5
2*y + 5
2*y + 8

Substituting values into Expressions

Now that we know how to create and modify expressions in SymPy, let’s take a look at how to evaluate them. What we need to do here, is use the subs() function to substitute the symbols with numerical values.

expr1 = 2*(x**2) + 5  # 2(x^2) + 5
print("Expr 1: (x=2)  ", expr1.subs(x, 2))
Expr 1: (x=2)   13

Now let’s try this for an expression which has multiple unknowns.

expr2 = 2*x + 4*y     # 2x + 2y
print("Expr 2: (x=2, y=4)  ", expr2.subs( {x: 2, y: 4} ))
Expr 2: (x=2, y=4)   20

We can also substitute them with other symbols, if that’s what we want.

x, y, a, b = symbols("x y a b")

expr3 = x**2 + y**2   # x^2 + y^2
print(expr3.subs({x: a, y: b}))
a**2 + b**2

Solving Equations with SymPy (root finding)

What’s even cooler, is that SymPy can literally solve entire equations for you, and return the root(s). No need to code the entire thing yourself, just use a single function along with the SymPy expression and a list of root(s) will be returned.

Let’s try to use the SymPy solve() on the expression x2 - x - 6.

import sympy
from sympy import symbols
x, y = symbols("x y")

expr = x**2 - x - 6  

[-2, 3]

Let’s try this out on another expression.

expr = (x + 1)*(x - 1)*(x + 5) 

x**3 + 5*x**2 - x - 5
[-5, -1, 1]

In the next section, we will cover several such operations, and explain how you can use them in your SymPy code.

Trigonometry with SymPy

Trigonometry is a pretty deal in most of Mathematica, so you might be wondering how you can include trigonometric functions and identities within your mathematical expressions. Let’s take a look!

Here’s a simple expression, sin(x). Let’s plug in a few values, just to verify the output. (These input values of x are in radians)

from sympy import symbols, expand, solve, trigsimp
from sympy import sin, cos, tan, acos, asin, atan, sinh, cosh, tanh, sec, cot, csc 

x, y = symbols("x y")
expr = sin(x)

print(expr.subs(x, 0))
print(expr.subs(x, (90/57.3)))

As you have probably already noticed from the imports in the previous code example, SymPy gives us access to all the different variants of the trigonometric functions. “asin” represents inverse sin (arc sin), whereas “sinh” represents hyperbolic sin. The same pattern applies to cos and tan as well.

Another cool thing we can do SymPy, is to simplify trigonometric identities. (No need to memorize any of them anymore!) SymPy will automatically attempt to simplify any expression you pass into the trigsimp() function. Let’s take a look at a few examples.

x, y = symbols("x y")

print(trigsimp(sin(x)**2 + cos(x)**2))
print(trigsimp(sin(x)**4 - 2*cos(x)**2*sin(x)**2 + cos(x)**4))
cos(4*x)/2 + 1/2

Differentiation and Integration in SymPy

The last main topic we will discuss in this Tutorial is how to differentiate and integrate expressions in Python SymPy.


In order to differentiate expressions using SymPy, we can use the diff() method on any expressions Depending on the type of parameters passed to diff(), it will return the differential of that expression.

The first parameter for diff() is the expression that you want to differentiate. The second parameter is what you wish to derivative with respect to. e.g: “Derivate with respect to x”.

Let’s take a look at an example.

expr = x**2 - x - 6
print(diff(expr, x))
2*x - 1

If you wish to differentiate an expression multiple times, there are two ways of doing so. The first method is by simply including the symbol you wish to derivate with respect to, multiple times.

expr = x**4

print(diff(expr, x))
print(diff(expr, x, x))
print(diff(expr, x, x, x))

Alternatively, you can include an integer “n” as a parameter after the symbol, and it will differentiate it “n” times.

expr = x**4

print(diff(expr, x, 1))
print(diff(expr, x, 2))
print(diff(expr, x, 3))

Furthermore, you can also differentiate with respect to multiple symbols within a single diff() function.

expr = y*x**2 + x*y**2
print(diff(expr, x, y))
2*(x + y)

Integration with SymPy

It’s time for Integration with SymPy. Let’s take a look at how we can integrate various mathematical expressions and obtain their integral forms.

Similarly to how differentiation works, we have a function for integration in SymPy called integrate(). It also takes the same parameters, which is the symbol by which we wish to integrate the expression by.

Let’s take a look at some examples.

from sympy import symbols, diff, integrate
x, y = symbols("x y")

expr = 2*x
print(integrate(expr, x))

A slightly more complex expression being integrated.

expr = x**2 - x - 6
print(integrate(expr, x))
x**3/3 - x**2/2 - 6*x

Here’s what happens when you integrate an expression with multiple symbols, with respect to just one of those symbols.

expr = x + y + 2
print(integrate(expr, x))
x**2/2 + x*(y + 2)

For more information, check out our dedicated tutorial on Differentiation and Integration with SymPy.

Python SymPy Tutorial Series

Is that all there is to SymPy though? Of course not! There are many more advanced features and functions yet to be covered.

Here is a complete breakdown of all the individual concepts that we have covered in Python SymPy on this website, along with links to their dedicated tutorials.

  1. Python SymPy Installation Guide
  2. Mathematical Expressions in SymPy
  3. Differentials and Integrals in SymPy
  4. Converting Strings to SymPy Expressions
  5. Trigonometric Functions in SymPy
  6. Matrices in SymPy
  7. Limits with SymPy

This marks the end of the Python SymPy Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Notify of
Inline Feedbacks
View all comments