Wednesday, June 10, 2026
HomeLanguagesPython | sympy.diff() method

Python | sympy.diff() method

With the help of sympy.diff() method, we can find the differentiation of mathematical expressions in the form of variables by using sympy.diff() method.

Syntax : sympy.diff(expression, reference variable) Return : Return differentiation of mathematical expression.

Example #1: In this example, we can see that by using sympy.diff() method, we can find the differentiation of mathematical expression with variables. Here we use the symbols() method also to declare a variable as a symbol. 

Python3




# import sympy
from sympy import *
 
x, y = symbols('x y')
gfg_exp = x + y
 
exp = expand(gfg_exp**2)
print("Before Differentiation : {}".format(exp))
 
# Use sympy.diff() method
dif = diff(exp, x)
 
print("After Differentiation : {}".format(dif))


Output :

Before Differentiation : x**2 + 2*x*y + y**2

 After Differentiation : 2*x + 2*y

  Example #2 : 

Python3




# import sympy
from sympy import *
x, y = symbols('x y')
gfg_exp = sin(x)*cos(x)
 
print("Before Differentiation : {}".format(gfg_exp))
 
# Use sympy.diff() method
dif = diff(gfg_exp, x)
 
print("After Differentiation : {}".format(dif))


Output :

Before Differentiation : sin(x)*cos(x) 

After Differentiation : -sin(x)**2 + cos(x)**2

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS