How can I solve equations in Python?

user1221937 picture user1221937 · May 8, 2012 · Viewed 124.5k times · Source

Let's say I have an equation:

2x + 6 = 12

With algebra we can see that x = 3. How can I make a program in Python that can solve for x? I'm new to programming, and I looked at eval() and exec() but I can't figure out how to make them do what I want. I do not want to use external libraries (e.g. SAGE), I want to do this in just plain Python.

Answer

eumiro picture eumiro · May 8, 2012

How about SymPy? Their solver looks like what you need. Have a look at their source code if you want to build the library yourself…