+

Python | sympy.as_coefficients_dict () method

Using the sympy.as_coefficients_dict() method, we can find the coefficients of all the elements in the dictionary that exist in mathematical expression using sympy.as_coefficients_dict () .

Syntax: sympy.as_coefficients_dict ()
Return: Return the coefficients of mathematical expression in the dictionary.

Example # 1:
In this example, we see that using sympy.as_coefficients_dict ( ) we can find all the coefficients in the math expression and return its dictionary.

# import sympy

from sympy import *  

 

x = symbols ( ` x` )

 
# Use the sympy method. as_coefficients_dict ()

gfg = x * * 3 + 21 * x + 12

  

print (gfg.as_coefficients_dict ())

Output:

defaultdict (, {1: 12, x ** 3: 1, x: 21 })

Example # 2:

# import sympy

from sympy import *  

  

x = symbols ( `x` )

 
# Use the sympy.as_coefficients_dict () method

gfg = x * * 3 + 21 * x * * 2 + 121 * x + 4

 

print (gfg.as_coefficients_dict ())

Output:

defaultdict (, {1: 4, x ** 3 : 1, x ** 2: 21, x: 121})

Get Solution for free from DataCamp guru