# Python | sympy.collect() method

With the help of `sympy.collect()` method, we are able to collect the mathematical expressions having same power.

Syntax : `sympy.collect()`
Return : Return mathematical expression having same powers.

Example #1 :
In this example, we can see that by using `sympy.collect()` method, all the same powers of variables are collected together as a same 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.collect() method ` `gfg_exp ``=` `collect(gfg_exp, x) ` ` `  `print``(gfg_exp) `

Output :

x**3 + x**2*(2 – z) + x*(y + 1) – 3

Example #2 :

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

Output :

x**3 + x**2*(1 – y) + x + 2*z**2 – 3

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.