To work with symbolic mathematics in Python we have chosen to use the libary Sympy. More detailed Sympy documentation and packages for installation can be found on http://sympy.org/.
Symbols
To make symbolic variables in Sympy you have to declare the variable explicitly.
>>> from sympy import * >>> x = Symbol('x') >>> y = Symbol('y')
Then may then manipulate them as you want.
>>> x + y + x - y 2*x >>> (x + y)**2 (x + y)**2 >>> ((x + y)**2).expand() x**2 + 2*x*y + y**2
You can also substitute variables for numbers or other symbolic variables with subs(var, substitution).
>>> ((x + y)**2).subs(x, 1) (y + 1)**2 >>> ((x + y)**2).subs(x, y) 4*y**2