# How to find the square of the binomial in Python

To calculate the square of a binomial in the Python language, you can use the expand() function of the Sympy library

**expand(x)**

The parameter x is an algebraic expression to be calculated, a binomial (a + b) raised to the power 2.

$$ ( a + b )^2 $$

The expand() function calculates the power of the binomial.

**Note**. The expand() function is located in an external module. So, to use it, you need to import it into your script using the import or from import statement.

## Example

To calculate the square of the binomial

$$ (x-1)^2 $$

You must define x as a symbol using the Symbol() function before defining the algebraic expression.

To calculate the square of the binomial use the function **expand()**

from sympy import Symbol, expand

x=Symbol('x')

e=(x-1)**2

expand(e)

The output result is the following:

x**2 - 2*x + 1

The output expression is equivalent to the algebraic result

$$ x^2 - 2x + 1 $$

**Report an error or share a suggestion to enhance this page**