# How to Find the Integral of a Function in Python

In this article, we show how to find the integral of a function in Python.

With modules, it is easy to find the integral of a mathematical function in Python.

The model we use is the sympy module.

In order to use this module, you must first install it.

So, open up the command prompt window on your computer and specify the full path to the Scripts folder in the Python package you installed. Then, type in, pip install sympy. Once you see that the module has been successfully installed, then you are ready to proceed to the code on this page.

So, below we will find the integral of the function, x^{2} + 8.

The Python code below calculates the integral of this function.

So, the first thing, we must do is import Symbol and Integral from the sympy module.

As explained above, this module must be installed by you.

The next thing we must do is specify the variable (or symbol) that we want to integrate with respect to. In our function, the independent variable is x. Therefore, we integrate with respect to the variable, x. This must always be specified when integrating in Python using the sympy module.

Next, we specify the function that we want to integrate. Again, we are integrating the function,
x^{4} + 7x^{3 + 8.
}

Translating this function into Python is, (x**2)+8.

Anytime a number is raised to a power, in Python the symbol, **, denotes this. Thus, x**4 represents x^{4}

Whenever we have a number multiplied by a variable, such as 7x, this must be specified with the symbol, *. Thus, 7x would be represented as 7*x.

We then create a variable named integralex (can be any name) and set it equal to Integral(function, x). The first parameter is the function you want to integrate and the second parameter is the variable, or symbol, that you want to integrate with respect to.

The last step is simply to call the doit() function on the integralex variable.

Running this code gives us the following output, shown below.

So, in the above example, we calculate the indefinite integral. This is an integral that is computed that has no lower or upper limit specified.

We will now do an example with an definite integral, which we specify a lower and upper limit.

We will do the same function we did as above but we will integrate it with respect to a lower limit of 2 and an upper limit of 4.

The code to do this is shown below.

This substitutes the upper and lower limits into the integral of the function. The value with the upper limit substituted into the integral minus the lower limit substituted into the integral gives us the final result.

Solving the definite integral above gives us the following output.

And this is all that is required to find the integral of a function in Python, either an indefinite integral or
a definite integral.

Related Resources

How to Show All Tables of a MySQL Database in Python

How to Count the Number of Rows in a MySQL Table in Python