You have four options

  1. Finite Differences
  2. Automatic Derivatives
  3. Symbolic Differentiation
  4. Compute derivatives by hand.

Finite differences require no external tools but are prone to numerical error and, if you're in a multivariate situation, can take a while.

Symbolic differentiation is ideal if your problem is simple enough. Symbolic methods are getting quite robust these days. SymPy is an excellent project for this that integrates well with NumPy. Look at the autowrap or lambdify functions or check out Jensen's blogpost about a similar question.

Automatic derivatives are very cool, aren't prone to numeric errors, but do require some additional libraries (google for this, there are a few good options). This is the most robust but also the most sophisticated/difficult to set up choice. If you're fine restricting yourself to numpy syntax then Theano might be a good choice.

Here is an example using SymPy

In [1]: from sympy import *
In [2]: import numpy as np
In [3]: x = Symbol('x')
In [4]: y = x**2 + 1
In [5]: yprime = y.diff(x)
In [6]: yprime
Out[6]: 2⋅x

In [7]: f = lambdify(x, yprime, 'numpy')
In [8]: f(np.ones(5))
Out[8]: [ 2.  2.  2.  2.  2.]
Answer from MRocklin on Stack Overflow
🌐
SciPy
docs.scipy.org › doc › scipy › reference › generated › scipy.differentiate.derivative.html
derivative — SciPy v1.17.0 Manual
Scientific Python Forum · Search ... step_factor=2.0, step_direction=0, preserve_shape=False, callback=None)[source]# Evaluate the derivative of an elementwise, real scalar function numerically....
Top answer
1 of 9
208

You have four options

  1. Finite Differences
  2. Automatic Derivatives
  3. Symbolic Differentiation
  4. Compute derivatives by hand.

Finite differences require no external tools but are prone to numerical error and, if you're in a multivariate situation, can take a while.

Symbolic differentiation is ideal if your problem is simple enough. Symbolic methods are getting quite robust these days. SymPy is an excellent project for this that integrates well with NumPy. Look at the autowrap or lambdify functions or check out Jensen's blogpost about a similar question.

Automatic derivatives are very cool, aren't prone to numeric errors, but do require some additional libraries (google for this, there are a few good options). This is the most robust but also the most sophisticated/difficult to set up choice. If you're fine restricting yourself to numpy syntax then Theano might be a good choice.

Here is an example using SymPy

In [1]: from sympy import *
In [2]: import numpy as np
In [3]: x = Symbol('x')
In [4]: y = x**2 + 1
In [5]: yprime = y.diff(x)
In [6]: yprime
Out[6]: 2⋅x

In [7]: f = lambdify(x, yprime, 'numpy')
In [8]: f(np.ones(5))
Out[8]: [ 2.  2.  2.  2.  2.]
2 of 9
82

The most straight-forward way I can think of is using numpy's gradient function:

x = numpy.linspace(0,10,1000)
dx = x[1]-x[0]
y = x**2 + 1
dydx = numpy.gradient(y, dx)

This way, dydx will be computed using central differences and will have the same length as y, unlike numpy.diff, which uses forward differences and will return (n-1) size vector.

🌐
Turing
turing.com › kb › derivative-functions-in-python
A Quick Guide to Calculating Derivatives in Python
These examples will showcase the versatility of Python for computing derivatives and highlight the applicability of derivative calculations in different domains. Derivative calculations for different types of functions ... Below are a few code snippets that demonstrate the methods discussed earlier. The functions and input values in these examples can be adapted and modified to specific requirements. 1. Numerical differentiation using central difference
🌐
Svitla Systems
svitla.com › home › articles › numerical differentiation methods in python
Python for Numerical Differentiation: Methods & Tools
January 14, 2021 - Error Estimate with an Analytical Form of Differential. Conclusion. Numerical differentiation is based on the approximation of the function from which the derivative is taken by an interpolation polynomial.
Price   $$$
Address   100 Meadowcreek Drive, Suite 102, 94925, Corte Madera
🌐
Towards Data Science
towardsdatascience.com › home › latest › taking derivatives in python
Taking Derivatives in Python | Towards Data Science
January 28, 2025 - To start, let’s take the most basic two-variable function and calculate partial derivatives. The function is simply – x squared multiplied by y, and you would differentiate it as follows: Cool, but how would I do this in Python? Good question.
🌐
Patrickwalls
patrickwalls.github.io › mathematicalpython › differentiation › differentiation
Numerical Differentiation - Mathematical Python - Patrick Walls
Let's write a function called derivative which takes input parameters f, a, method and h (with default values method='central' and h=0.01) and returns the corresponding difference formula for $f'(a)$ with step size $h$.
🌐
Blog
halvorsen.blog › documents › programming › python › resources › powerpoints › Numerical Differentiation in Python.pdf pdf
Numerical Differentiation in Python Hans-Petter Halvorsen
We will use numerical differentiation · to find !' !$ for the following function: 𝑦𝑥= 𝑥& x · dy/dx · -2 · -1 · 0 · 1 · 2 · We use the following · data points: Results: dydx_exact= [-4 -2 0 2 4] dydx_num [-3. -1. 1. 3.] Comments to the Results · 𝑦𝑥= 𝑥# 𝑑𝑦 · 𝑑𝑥=? Exact Solution vs. Python Solution: dydx_exact= [-4 -2 0 2 4] dydx_num [-3.
Find elsewhere
🌐
Medium
medium.com › @rajat01221 › how-to-create-a-python-program-to-solve-differentiation-2a90cc1cef8f
How to Create a Python Program to Solve Differentiation | by Rajat Sharma | Medium
July 25, 2024 - Creating a Python program to solve ... methods. SymPy provides a robust framework for symbolic differentiation, while NumPy enables numerical differentiation for discrete data points or more complex functions....
🌐
GitHub
github.com › Ryota-Kawamura › Mathematics-for-Machine-Learning-and-Data-Science-Specialization › blob › main › Course-2 › Week-1 › C2_W1_Lab_1_differentiation_in_python.ipynb
Mathematics-for-Machine-Learning-and-Data-Science-Specialization/Course-2/Week-1/C2_W1_Lab_1_differentiation_in_python.ipynb at main · Ryota-Kawamura/Mathematics-for-Machine-Learning-and-Data-Science-Specialization
"In this lab you explore which tools and libraries are available in Python to compute derivatives. You will perform symbolic differentiation with `SymPy` library, numerical with `NumPy` and automatic with `JAX` (based on `Autograd`). Comparing the speed of calculations, you will investigate the computational efficiency of those three methods." ... "This is just a reminder how to define functions in Python.
Author   Ryota-Kawamura
🌐
AskPython
askpython.com › home › derivatives in python using sympy
Derivatives in Python using SymPy - AskPython
August 6, 2022 - We can use SymPy library to calculate derivatives in Python. We can calculate differentiation, derivative, partial derivative using diff(), lambdify().
🌐
DEV Community
dev.to › erikwhiting88 › calculate-derivative-functions-in-python-h58
Derivative Python: Calculate Derivative Functions in Python - DEV Community
September 3, 2019 - If you have a function that can be expressed as f(x) = 2x^2 + 3 then the derivative of that function, or the rate at which that function is changing, can be calculated with f'(x) = 4x. Note: In case you don't know, the f'(x) is pronounced "f prime of x" Derivatives have a lot of use in tons of fields, but if you're trying to figure out how to calculate one with Python, you probably don't need much more explanation from me, so lets just dive in.
🌐
Learning About Electronics
learningaboutelectronics.com › Articles › How-to-find-the-derivative-of-a-function-in-Python.php
How to Find the Derivative of a Function in Python
Anytime a number is raised to a power, in Python the symbol, **, denotes this. Thus, x**4 represents x4 · 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.
🌐
Python Like You Mean It
pythonlikeyoumeanit.com › Module3_IntroducingNumpy › AutoDiff.html
Automatic Differentiation — Python Like You Mean It
MyGrad takes this one step further, and provides true drop-in automatic differentiation to NumPy. Install MyGrad into your Python environment. Open your terminal, activate your desired Python environment, and run the following command. ... Let’s jump right in with a simple example of using MyGrad to evaluate the derivative of a function at a specific point.
🌐
SciPy
docs.scipy.org › doc › scipy › reference › differentiate.html
Finite Difference Differentiation (scipy.differentiate) — SciPy v1.17.0 Manual
Scientific Python Forum · Search Ctrl+K · SciPy differentiate provides functions for performing finite difference numerical differentiation of black-box functions.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-sympy-derivative-method
Python | sympy.Derivative() method - GeeksforGeeks
July 12, 2025 - Python3 · # import sympy from sympy import * # Derivative method for trigonometric functions x, y = symbols('x') expr = sin(x) + cos(x) print("Expression : {}".format(expr)) # Use sympy.Derivative() method expr_diff = Derivative(expr, x) print("Derivative of expression with respect to x : {}".format(expr_diff)) print("Value of the derivative : {}".format(expr_diff.doit())) Output: Expression : sin(x) + cos(x) Derivative of expression with respect to x : Derivative(sin(x) + cos(x), x) Value of the derivative : -sin(x) + cos(x) Comment ·
🌐
Delft Stack
delftstack.com › home › howto › python › python derivative
How to Calculate Derivative in Python | Delft Stack
February 26, 2025 - Learn how to calculate derivatives in Python using the SymPy library. This article provides step-by-step instructions and code examples for differentiating simple and complex functions, including polynomials and trigonometric functions. Discover how to use SymPy's lambdify function for numerical evaluations, making your mathematical computations in Python easier and more efficient.
🌐
SciPy
docs.scipy.org › doc › scipy-1.16.2 › reference › generated › scipy.differentiate.derivative.html
derivative — SciPy v1.16.2 Manual
Scientific Python Forum · Search ... step_factor=2.0, step_direction=0, preserve_shape=False, callback=None)[source]# Evaluate the derivative of a elementwise, real scalar function numerically....
🌐
Berkeley
pythonnumericalmethods.berkeley.edu › notebooks › chapter20.00-Numerical-Differentiation.html
Chapter 20. Numerical Differentiation — Python Numerical Methods
However, in practice the function may not be explicitly known, or the function may be implicitly represented by a set of data points. In these cases and others, it may be desirable to compute derivatives numerically rather than analytically. The focus of this chapter is numerical differentiation.