Open In App

Python | sympy.coeff(x, n) method

With the help of sympy.coeff(x, n) method, we are able to find the coefficient of variables in mathematical expressions.

Syntax : sympy.coeff(x, n)
Return : Return the coefficient of variables.



Example #1 :
In this example, we can see that by using sympy.coeff(x, n) method, we can find the coefficient of mathematical expression.




# import sympy
from sympy import * x, y, z = symbols('x y z')
gfg_exp = x * y + x - 3 + 2 * x**2 - z * x**2 + x**3
  
# Using sympy.coeff(x, n) method
gfg_exp = gfg_exp.coeff(x, 2)
  
print(gfg_exp)

Output :

2 – z



Example #2 :




# import sympy
from sympy import * x, y, z = symbols('x y z')
gfg_exp = z * x + x - 3 + 2 * x**2 - y * x**2 + y**3
  
# Using sympy.coeff(x, n) method
gfg_exp = gfg_exp.coeff(y, 1)
  
print(gfg_exp)

Output :

-x**2

Article Tags :