# Python sympy.coeff(x, n) Method

Python SymPy Module:

SymPy is a Python symbolic mathematics library. It aims to be a full-featured computer algebra system (CAS) while keeping the code as basic(simple) as possible in order to be understandable and easily expandable. SymPy is entirely written in Python. SymPy is simple to use because it only depends on mpmath, a pure Python library for arbitrary floating-point arithmetic.

Rational and Integer are the numerical types defined by SymPy. A rational number is represented by the Rational class as a pair of two Integers, numerator and denominator, therefore Rational(1, 2) is 1/2, Rational(3, 2) is 3/2, and so on. Integer numbers are represented by the Integer class.

SymPy uses mpmath in the background, allowing it to execute arbitrary-precision arithmetic computations. Some special constants, such as exp, pi, and oo (Infinity), are thus considered as symbols and can be evaluated with arbitrary precision.

Installation:

pip install sympy

Python sympy.coeff(x, n) Method:

We can determine the coefficient of variables in mathematical expressions using the coeff(x, n) method of the sympy module.

Syntax:

 sympy.coeff(x, n)

Return Value:

The coefficient of variables is returned by the coeff() function.

## sympy.coeff(x, n) Method in Python

Example1

Here it gives the coefficient of x^2 in the given mathematical expression i.e, 1+3 = 4

Approach:

• Import all the functions from sympy module using the import keyword
• Pass the symbols to be used as arguments to the symbols() function and store them in corresponding variables.
• Give the mathematical expression as static input and store it in a variable.
• Pass the variable and power as arguments to the coeff() method and apply it to the given expression to get the coefficient of the given mathematical expression.
• Here it gives the coefficient of x^2 i.e, 1+3 = 4
• Store it in another variable.
• Print the coefficient of x^2 in the given mathematical expression.
• The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Pass the symbols to be used as arguments to the symbols() function
# and store them in corresponding variables.
x, y, z = symbols('x y z')

# Give the mathematical expression as static input and store it in a variable.
gvn_expression = x**2+3*y**2+3*x**2+x+y

# Pass the variable and power as arguments to the coeff() method and apply it on the
# given expression to get the coefficient of given mathematical expression.
# Here it gives the coefficient of x^2 i.e, 1+3 = 4
# Store it in another variable.
rslt = gvn_expression.coeff(x, 2)

# Print the coefficient of x^2 in the given mathematical expression.
print("The coefficient of x^2 in the given mathematical expression is:")
print(rslt)


Output:

The coefficient of x^2 in the given mathematical expression is:
4

Example2

Here it gives the coefficient of y^3 in the given mathematical expression i.e, 1+6-1 = 6

Approach:

• Import all the functions from sympy module using the import keyword
• Pass the symbols to be used as arguments to the symbols() function and store them in corresponding variables.
• Give the mathematical expression as static input and store it in a variable.
• Pass the variable and power as arguments to the coeff() method and apply it to the given expression to get the coefficient of the given mathematical expression.
• Here it gives the coefficient of y^3 i.e, 1+6-1 = 6
• Store it in another variable.
• Print the coefficient of y^3 in the given mathematical expression.
• The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Pass the symbols to be used as arguments to the symbols() function
# and store them in corresponding variables.
x, y, z = symbols('x y z')

# Give the mathematical expression as static input and store it in a variable.
gvn_expression = y**3+5*x**2+6*y**3+x-y**3+z

# Pass the variable and power as arguments to the coeff() method and apply it to the
# given expression to get the coefficient of given mathematical expression.
# Here it gives the coefficient of y^3 i.e, 1+6-1 = 6
# Store it in another variable.
rslt = gvn_expression.coeff(y, 3)

# Print the coefficient of y^3 in the given mathematical expression.
print("The coefficient of y^3 in the given mathematical expression is:")
print(rslt)


Output:

The coefficient of y^3 in the given mathematical expression is:
6