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 sympyfrom sympy import *Â
x, y = symbols('x y')gfg_exp = x + yÂ
exp = expand(gfg_exp**2)print("Before Differentiation : {}".format(exp))Â
# Use sympy.diff() methoddif = 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 sympyfrom sympy import *x, y = symbols('x y')gfg_exp = sin(x)*cos(x)Â
print("Before Differentiation : {}".format(gfg_exp))Â
# Use sympy.diff() methoddif = diff(gfg_exp, x)Â
print("After Differentiation : {}".format(dif)) |
Output :
Before Differentiation : sin(x)*cos(x)Â
After Differentiation : -sin(x)**2 + cos(x)**2
