How to calculate algebraic expansion of power of a binomial in Python
To solve the nth power of a binomial (a+b)n o (a-b)n in python, you can use the expand() function of the sympy module.
expand(x)
The parameter x is the binomial of power in symbolic or numerical form.
$$ (a+b)^n $$
The expand() function calculates the algebraic expansion of the binomial and returns it as output.
Note. If the binomial is also composed of variables x, y, z, they must be previously defined as symbols.
Example
To calculate the power of the binomial
$$ (x-3)^4 $$
The variable x is defined as a symbol using the Symbol () function. The algebraic expression is assigned to the variable b.
The expand () function algebraically expands the power of the binomial.
from sympy import Symbol, expand
x=Symbols('x')
b=(x-3)**4
expand(b)
The function outputs the result in symbolic form
x**4 - 12*x**3 + 54*x**2 - 108*x + 81
The result is equivalent to the algebraic expression
$$ x^4 - 12x^3 + 54x^2 - 108x +81 $$