How to Perform Indefinite (Symbolic) Integration in Python Using SymPy ?

Introduction

You can perform indefinite integration (symbolic integration) easily in Python using SymPy, which is a symbolic mathematics library.

Step 1. Import the necessary modules

1
import sympy as sp

Step 2. Define the symbol(s)

You first define the variable of integration using sp.Symbol or sp.symbols:

1
x = sp.Symbol('x')

Step 3. Define the function to integrate

Let’s say we want to integrate ( f(x) = x^2 * e^x ):

1
f = x**2 * sp.exp(x)

Step 4. Compute the indefinite integral

Use sp.integrate():

1
2
F = sp.integrate(f, x)
print(F)

Output:

1
x**2*exp(x) - 2*x*exp(x) + 2*exp(x)

How to Perform Indefinite (Symbolic) Integration in Python Using SymPy ?
How to Perform Indefinite (Symbolic) Integration in Python Using SymPy ?

(SymPy omits the “+ C” constant by default.)

Step 5. (Optional) Add the constant of integration

If you want to display it explicitly:

1
2
3
C = sp.Symbol('C')
F_with_C = F + C
print(F_with_C)

Example with multiple variables

If your expression has multiple variables, you can specify which one to integrate with respect to:

1
2
3
4
5
6
7
y = sp.Symbol('y')
expr = x**2 + sp.sin(y)
integral_x = sp.integrate(expr, x)
integral_y = sp.integrate(expr, y)

print("∫ wrt x:", integral_x)
print("∫ wrt y:", integral_y)

Example: Trigonometric integral

1
2
3
f = sp.sin(x) * sp.cos(x)
F = sp.integrate(f, x)
print(F.simplify())

Output:

1
sin(x)**2/2

Rendering SymPy Expressions in LaTeX (Jupyter Notebook)

In a Jupyter notebook, you can render SymPy expressions beautifully in LaTeX using the built-in display tools.
This allows symbolic results such as integrals or equations to appear as properly formatted mathematical expressions, improving readability and presentation quality.

This automatically makes all SymPy outputs render in LaTeX format.

1
2
3
4
5
6
7
8
9
import sympy as sp

sp.init_printing()   # enables nice LaTeX output

x = sp.Symbol('x')
f = x**2 * sp.exp(x)

F = sp.integrate(f, x)
F

Output:

You’ll see something like

How to Perform Indefinite (Symbolic) Integration in Python Using SymPy ?
How to Perform Indefinite (Symbolic) Integration in Python Using SymPy ?

nicely rendered as LaTeX in the notebook output cell.

Option 2: Use display() and sp.Eq()

If you want to show an equation-style output:

1
2
3
4
5
6
7
8
from sympy import symbols, integrate, exp, Eq
from IPython.display import display

x = symbols('x')
f = x**2 * exp(x)
F = integrate(f, x)

display(Eq(sp.Integral(f, x), F))

Output:

How to Perform Indefinite (Symbolic) Integration in Python Using SymPy ?
How to Perform Indefinite (Symbolic) Integration in Python Using SymPy ?

Option 3: Use display(Math(...)) explicitly

For full manual control of LaTeX rendering:

1
2
3
from IPython.display import display, Math

display(Math(f"\\int x^2 e^x \\,dx = {sp.latex(F)} + C"))

This uses SymPy’s latex() function to convert symbolic expressions into valid LaTeX strings.

How to Perform Indefinite (Symbolic) Integration in Python Using SymPy ?
How to Perform Indefinite (Symbolic) Integration in Python Using SymPy ?

Option 4: (Optional) Inline LaTeX with Markdown cell

You can also insert a Markdown cell and use something like:

1
$\int x^2 e^x \,dx = e^x(x^2 - 2x + 2) + C$

for presentation-quality output.

How to Perform Indefinite (Symbolic) Integration in Python Using SymPy ?
How to Perform Indefinite (Symbolic) Integration in Python Using SymPy ?

References

Links Site
https://docs.sympy.org/latest/modules/integrals/integrals.html SymPy Integrals Documentation — official reference for integrate(), Integral objects, and symbolic integration.
https://docs.sympy.org/latest/tutorial/printing.html SymPy Printing (LaTeX Rendering) — explains init_printing(), latex(), and pretty-printing in Jupyter.
https://ipython.readthedocs.io/en/stable/api/generated/IPython.display.html IPython Display Module — official documentation for display(), Math(), and rich LaTeX output in notebooks.
https://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/Working%20With%20Markdown%20Cells.html Jupyter Notebook Markdown Guide — how to render LaTeX equations in Markdown cells.
https://docs.python.org/3/library/sympy.html Python SymPy Module (Standard Library Index) — overview of SymPy as a symbolic mathematics library.
Image

of