Python | sympy.replace() method

With the help of sympy.replace() method, we can replace the functions in the mathematical expression without editing the whole expression by using sympy.replace() method.

Syntax : sympy.replace()
Return : Return the replaced values in the mathematical expression.

Example #1 :
In this example we can see that by using sympy.replace() method, we are able to replace the mathematical functions in the expression and return the updated expression.




# import sympy
from sympy import * 
  
x, y = symbols('x y')
  
f = sin(x) + cos(2 * x)
  
# Use sympy.replace() method
gfg = f.replace(cos, sin)
     
print(gfg)


Output :

sin(x) + sin(2*x)

Example #2 :




# import sympy
from sympy import * 
  
x, y = symbols('x y')
  
f = log(sin(x)) + tan(cos(2 * x))
# Use sympy.replace() method
gfg = f.replace(log, tan)
     
print(gfg)


Output :

tan(sin(x)) + tan(cos(2*x))