Friday, December 12, 2025
HomeLanguagesPython | sympy.expand() method

Python | sympy.expand() method

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.

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 expand() method from sympy
exp = 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 expand() method from sympy
exp = expand(gfg_exp**2)
print(exp)


Output :

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

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32445 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6813 POSTS0 COMMENTS
Nicole Veronica
11952 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12029 POSTS0 COMMENTS
Shaida Kate Naidoo
6949 POSTS0 COMMENTS
Ted Musemwa
7199 POSTS0 COMMENTS
Thapelo Manthata
6894 POSTS0 COMMENTS
Umr Jansen
6881 POSTS0 COMMENTS