Solving Equations

Solving Equations

SymPy's solve() function can be used to solve equations and expressions that contain symbolic math variables.

Equations with one solution

A simple equation that contains one variable like $x-4-2 = 0$ can be solved using the SymPy's solve() function. When only one value is part of the solution, the solution is in the form of a list.

The code section below demonstrates SymPy's solve() function when an expression is defined with symbolic math variables.

In :
from sympy import symbols, solve

x = symbols('x') expr = x-4-2

sol = solve(expr)

sol

Out:


To pull the value out of the solution list sol, regular list indexing can be used.
In :
num = sol

num

Out:
6

The code section below demonstrates SymPy's solve() function when an equation is defined with symbolic math variables.
In :
from sympy import symbols, Eq, solve

y = symbols('y') eq1 = Eq(y + 3 + 8)

sol = solve(eq1) sol

Out:
[-11]

Equations with two solutions

Quadratic equations, like $x^2 - 5x + 6 = 0$, have two solutions. SymPy's solve() function can be used to solve an equation with two solutions. When an equation has two solutions, SymPy's solve() function outputs a list. The elements in the list are the two solutions.

The code section below shows how an equation with two solutions is solved with SymPy's solve() function.

In :
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

Out:
[2, 3]

If you specify the keyword argument dict=True to SymPy's solve() function, the output is still a list, but inside the list is a dictionary that shows which variable was solved for.
In :
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1, dict=True) sol

Out:
[{x: 2}, {x: 3}]

In :
sol

Out:
{x: 2}

In :
sol

Out:
{x: 3}