Math and Science with Python

Solve Equations in Python with SymPy solve

Find exact solutions of equations and systems of equations with sympy.solve, from quadratics to complex roots.

  • Intermediate
  • 3 min read
  • 1 runnable example
What you'll learn
  • sp.solve with an expression or Eq
  • Exact roots, complex roots and decimals
  • Solving a system of equations
What sp.solve returns
x**2 - 5x + 6
  • [2, 3]
  • two real roots
x**2 + 1
  • [-I, I]
  • complex roots
x + y = 10, x - y = 2
  • {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.

From a balance scale to code
x = sp.symbols("x")⚖️ The unknown weight
sp.Eq(2*x + 3, 11)🧮 Both sides of the scale
sp.solve(..., x)🔍 Find every weight that balances it
[4]✅ The list of solutions
Every value that makes both sides equal.
In one sentence

sympy.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

  1. sp.solve(x**2 - 5*x + 6, x) returns [2, 3].
  2. sp.Eq(2*x + 3, 11) writes an equation whose right side is not 0.
  3. Roots stay exact, like 1 - sqrt(2); float() turns them into decimals.
  4. 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: use sp.nsolve or scipy.optimize.fsolve.
  • Large linear systems with numbers: np.linalg.solve is much faster.

Example code

Open in editor →
main.py
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)
Get a Run button for this example

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.

  1. Change the function or the data and predict the result before running.
  2. Compare the numerical answer with the exact one from SymPy or a hand calculation.
  3. Plot the result with Matplotlib to see what the numbers mean.
Run in the Python compiler →

Common mistakes

Writing an equation with ==

== compares two expressions and gives False; it does not make an equation.

Problem
sp.solve(2*x + 3 == 11, x)
Fix
sp.solve(sp.Eq(2*x + 3, 11), x)

Using ^ for powers

In Python ^ is bitwise XOR, not a power.

Problem
sp.solve(x^2 - 4, x)
Fix
sp.solve(x**2 - 4, x)

Finished this lesson?

Mark it as learned to track your progress.

Tools for this lesson