Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python | sympy.expand() method

  • Last Updated : 12 Jun, 2019

With the help of sympy.expand() method, we can expand the mathematical expressions in the form of variables by using sympy.expand() method.

Syntax : sympy.expand(expression)
Return : Return mathematical expression.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Example #1 :
In this example we can see that by using sympy.expand() method, we can get the mathematical expression with variables. Here we use symbols() method also to declare a variable as symbol.






# import sympy
from sympy import expand, symbols
  
x, y = symbols('x y')
gfg_exp = x + y
  
# Use sympy.expand() method
exp = sympy.expand(gfg_exp**2)
  
print(exp)

Output :

x**2 + 2*x*y + y**2

Example #2 :




# import sympy
from sympy import expand, symbols
  
x, y, z = symbols('x y z')
gfg_exp = x + y + z
  
# Use sympy.expand() method
exp = sympy.expand(gfg_exp**2)
  
print(exp)

Output :

x**2 + 2*x*y + 2*x*z + y**2 + 2*y*z + z**2




My Personal Notes arrow_drop_up
Recommended Articles
Page :