# How to Find the Derivative of a Function in Python

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

With modules, it is easy to find the derivative 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 derivative of the function, x^{4} + 7x^{3} + 8.

The Python code below calculates the derivative of this function.

So, the first thing, we must do is import Symbol and Derivative 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 differentiate with respect to. In our function, the independent variable is x. Therefore, we differentiate with respect to the variable, x. This must always be specified when differentiating in Python using the sympy module.

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

Translating this function into Python is, x**4 + 7*x**3 + 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 deriv (can be any name) and set it equal to Derivative(function, x). The first parameter is the function you want to differentiate and the second parameter is the variable, or symbol, that you want to differentiate with respect to.

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

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

You can also take this a step further. We'll solve the differentiation for the function. If we want to calculate the value of the derivative at a particular value of x- for example, when x=4, we use the subs() method.

This is shown below.

Running this code gives the following output shown below.

And this is all that is required to find the derivative of a function in Python.

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