Solving Equations
An equation says that two expressions are equal. Solving it means finding the value of the variable that makes the statement true. The method is like keeping a balance level: whatever you do to one side, you also do to the other. This lesson covers one-step and two-step equations and equations with the variable on both sides. It finishes by checking the work with SymPy.
The balance idea
Think of as a balance scale with on the left pan and on the right. The pans are level. To find we strip things off the left side until only remains, and we keep the scale level by doing exactly the same to the right side.
Every step uses an inverse operation, the operation that undoes another one:
| To undo | Do this to both sides |
|---|---|
| subtract 3 | |
| add 7 | |
| divide by 4 | |
| multiply by 5 |
Undo things in the reverse order of operations: first the additions and subtractions, then the multiplications and divisions.
Tip: Always check by substituting the answer into the original equation. That one line catches most sign mistakes.
Variables on both sides
If appears on both sides, first gather the terms on one side and the numbers on the other:
With brackets, distribute first. With fractions, multiply both sides by the LCM of the denominators so the fractions disappear:
Python's fractions module keeps every step exact, so you can follow the balance method without any rounding:
from fractions import Fraction as F
# solve a*x + b = c*x + d step by step
a, b, c, d = F(5), F(-4), F(2), F(11)
print(f"{a}x + ({b}) = {c}x + {d}")
a, c = a - c, 0
print(f"{a}x + ({b}) = {d} subtract {F(2)}x")
d, b = d - b, 0
print(f"{a}x = {d} add 4")
x = d / a
print(f"x = {x}")
# a fraction answer stays exact
print("3x + 1 = 8 -> x =", (F(8) - 1) / 3)5x + (-4) = 2x + 11
3x + (-4) = 11 subtract 2x
3x = 15 add 4
x = 5
3x + 1 = 8 -> x = 7/3
When there is no solution or every number works
Sometimes the variable cancels completely:
- becomes , which is false. There is no solution.
- becomes , which is always true. Every number is a solution (an identity).
Checking with SymPy
SymPy solves equations symbolically. Eq(left, right) builds an equation and solve returns a list of solutions. An empty list means no solution. Solve by hand first, then use SymPy as a check:
from sympy import symbols, Eq, solve, Rational
x = symbols("x")
print(solve(Eq(5*x - 4, 2*x + 11), x))
print(solve(Eq(x/3 + x/4, 7), x))
print(solve(Eq(3*x + 1, 8), x))
print(solve(Eq(2*x + 1, 2*x + 5), x))
print(solve(Eq(Rational(3, 4)*x - 2, Rational(1, 2)), x))[5]
[12]
[7/3]
[]
[10/3]
In software
Programmers rearrange equations all the time, usually to solve a formula for a different variable:
- Inverse lerp. Linear interpolation is . Solving for gives , which tells you how far along a progress bar, a slider or an animation a value is (see Lerp).
- Unit and temperature conversion. turned around gives .
- Time to reach a target. If health regenerates by 4 per second from 30, solving gives .
def inverse_lerp(a, b, v):
return (v - a) / (b - a)
print(inverse_lerp(0, 200, 50)) # 50 px along a 200 px bar
print(inverse_lerp(-10, 30, 20)) # 20 °C on a −10..30 scale
print((100 - 30) / 4, "seconds") # 30 + 4t = 1000.25
0.75
17.5 seconds
Exercises
Exercise 1: Two steps
Solve .
Answer
Solution
Exercise 2: Brackets on both sides
Solve .
Answer
Solution
Exercise 3: Clear the fractions
Solve .
Answer
Solution
The LCM of 2 and 5 is 10, so multiply every term by 10:
from sympy import symbols, Eq, solve
x = symbols("x")
print(solve(Eq(x/2 - 1, x/5 + 2), x))[10]
Exercise 4: Solve a formula
A phone plan costs €12 per month plus €0.05 per MB of data. Write the cost equation for a bill of €20 and find how many MB were used. Then write a Python function mb_used(bill) that solves it for any bill.
Answer
, so .
Solution
Solving for in general gives :
def mb_used(bill):
return (bill - 12) / 0.05
print(round(mb_used(20), 2), round(mb_used(31.5), 2))160.0 390.0
Test yourself
What is the first step to solve by the balance method?
- Divide both sides by 3
- Subtract 8 from both sides
- Add 8 to both sides
- Multiply both sides by 3
How many solutions does have?
- One
- Two
- None
- Infinitely many
Solving for gives: