Python | sympy.subs () method

Python Methods and Functions

Using the sympy.subs() method, we can substitute the values ​​of variables into various mathematical functions using the sympy.subs () method.

Syntax: sympy.subs (source, destination)
Return: Return the same expression by changing the variable.

Example # 1:

In this example, we see that with sympy.subs () we can substitute the value of an existing variable into a new variable.

# import sympy

from sympy import *

 
# symbol declaration

x, y = symbols ( `xy` )

 

gfg_exp = cos (x) + 1

 
# use sympy.subs () method

gfg_exp = gfg_exp.subs (x, y)

 

print (gfg_exp)

Output:

cos (y) + 1

Example # 2:

# import sympy

from sympy import *

 
# symbol declaration

x, y = symbols ( ` xy` )

 

gfg_exp = cos (x) + 1

 
# use the sympy.subs () method

gfg_exp = gfg_exp.subs (x, 0 )

  

print (gfg_exp)

Logout:

2





Get Solution for free from DataCamp guru