Solve Equations in Python with SymPy solve
Find exact solutions of equations and systems of equations with sympy.solve, from quadratics to complex roots.
- sp.solve with an expression or Eq
- Exact roots, complex roots and decimals
- Solving a system of equations
- [2, 3]
- two real roots
- [-I, I]
- complex roots
- {x: 6, y: 4}
- a system
Let SymPy do the algebra
The problem
Solving equations by hand is error-prone: a sign slips in the quadratic formula, or a system of equations takes a page of substitution. Checking homework answers or setting up a physics problem is quicker when the algebra is done for you.
Think of it like this
sympy.solve is like a balance scale puzzle solver. You put the equation on the scale, and it finds every value of the unknown weight that keeps both sides level.
x = sp.symbols("x")⚖️ The unknown weightsp.Eq(2*x + 3, 11)🧮 Both sides of the scalesp.solve(..., x)🔍 Find every weight that balances it[4]✅ The list of solutionssympy.solve(equation, unknown) returns the exact solutions of an equation as a list. An expression on its own is treated as equal to 0; use sp.Eq(left, right) for other equations.
Key points
sp.solve(x**2 - 5*x + 6, x)returns[2, 3].sp.Eq(2*x + 3, 11)writes an equation whose right side is not 0.- Roots stay exact, like
1 - sqrt(2);float()turns them into decimals. - A list of equations and a list of unknowns gives a dictionary such as
{x: 6, y: 4}.
When should you use it?
- Checking algebra and homework answers.
- Rearranging physics formulas for one variable.
- Finding intersection points of two curves.
When not to use it
- Equations SymPy cannot solve exactly, like
cos(x)= x: usesp.nsolveorscipy.optimize.fsolve. - Large linear systems with numbers:
np.linalg.solveis much faster.
Example code
import sympy as sp
x, y = sp.symbols("x y")
# A quadratic: x**2 - 5x + 6 = 0 (write it as expression = 0)
print("Roots of x**2 - 5x + 6:", sp.solve(x**2 - 5*x + 6, x))
# Use Eq when the right side is not 0: 2x + 3 = 11
print("2x + 3 = 11 ->", sp.solve(sp.Eq(2*x + 3, 11), x))
# No real roots: SymPy returns complex ones
print("x**2 + 1 = 0 ->", sp.solve(x**2 + 1, x))
# Keep the square roots exact, then get decimals with evalf
roots = sp.solve(x**2 - 2*x - 1, x)
print("x**2 - 2x - 1 = 0 ->", roots, "=", [round(float(r), 4) for r in roots])
# A system of two equations: x + y = 10, x - y = 2
solution = sp.solve([sp.Eq(x + y, 10), sp.Eq(x - y, 2)], [x, y])
print("System:", solution)
Result of running the code
Roots of x**2 - 5x + 6: [2, 3]
2x + 3 = 11 -> [4]
x**2 + 1 = 0 -> [-I, I]
x**2 - 2x - 1 = 0 -> [1 - sqrt(2), 1 + sqrt(2)] = [-0.4142, 2.4142]
System: {x: 6, y: 4}
Code explanation
sp.solve(x**2 - 5*x + 6, x) treats the expression as equal to 0 and returns the roots [2, 3]. For 2x + 3 = 11 the right side is not 0, so the equation is written as sp.Eq(2*x + 3, 11). x**2 + 1 has no real roots, so SymPy returns -I and I, the imaginary unit. Roots like 1 - sqrt(2) stay exact until float() turns them into decimals, and passing a list of equations and a list of unknowns solves the system, returning the dictionary {x: 6, y: 4}.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the function or the data and predict the result before running.
- Compare the numerical answer with the exact one from SymPy or a hand calculation.
- Plot the result with Matplotlib to see what the numbers mean.
Common mistakes
Writing an equation with ==
== compares two expressions and gives False; it does not make an equation.
sp.solve(2*x + 3 == 11, x)
sp.solve(sp.Eq(2*x + 3, 11), x)
Using ^ for powers
In Python ^ is bitwise XOR, not a power.
sp.solve(x^2 - 4, x)
sp.solve(x**2 - 4, x)
Finished this lesson?
Mark it as learned to track your progress.
Tools for this lesson
- Graph Plotter Online: Plot Points and FunctionsPlot points, a pasted table or functions like sin(x) online and download the graph as a PNG. Free graph maker that also gives you the Python Matplotlib code.
- How to Plot a Graph in Python Online (Matplotlib Examples)Plot a graph in Python online with Matplotlib: lines, functions like sin(x), data from a CSV, bar and pie charts. Run each example in your browser.
- NumPy Online: Run NumPy Code in Your BrowserA NumPy playground with no install: arrays, statistics, linear algebra, random numbers and broadcasting. NumPy 2, pandas and SciPy ready to import.