An Introduction to Python For Undergraduate Engineers/Symbolic Algebra

Before we can start any algebra, we must first tell Python what we want it to treat as symbols instead of numbers (x,y,z etc.). We do this in the following way, having imported the sympy module:

x = Symbol('x')

Note that it is important to use the capital S otherwise this won't work. What we are doing here, is telling Python to create a new variable x and to assign the value 'x' to it. This way, it won't treat it as a numeric value, but just the symbol x. The module sympy.abc contains all the letters of the alphabet as premade symbols.

We can then create a variable that is a function of x. In this case I shall call the variable that will contain the function, myfunction;

myfunction = x**2

If we ask python what the value of myfunction is (by typing just 'myfunction'), we get the equation that we put in. Now, we can substitute a value for x into the equation by using .subs(old value, new value), for example:

myfunction.subs(x,5)

This will replace x with 5, making the value of myfunction 25.