Velamuvi AIAcademy

Lesson 3 of 12 · Numbers and Algebra

Variables and Equations

Learn what variables, expressions and equations are, how to solve and rearrange simple equations step by step, and how to check answers with Python. A gentle algebra refresher for AI.

  • 8 min read
  • Beginner
  • Reviewed
In this lesson you will learn
  • What variables, expressions and equations are
  • The words coefficient and constant
  • How to solve a simple equation by keeping it balanced
  • How to rearrange a formula to find a different value
  • How to check an answer by substitution

Letters that stand for numbers

Algebra is maths with letters. A letter stands for a number that can change, or that we do not know yet.

In maths, 3x means 3 × x. The multiplication sign is left out. In Python, you must always write it: 3 * x.

The parts of an expression

In 3x + 2:

Sound familiar? The tiny house-price model from the first lesson, price = 50000 × size + 200000, has exactly this shape. The coefficient and the constant are its parameters.

Evaluating an expression

Try it yourselfPython
def expression(x):
    return 3 * x + 2


for x in [0, 1, 4, -2]:
    print(f"x = {x}: 3x + 2 = {expression(x)}")

Solving an equation: keep it balanced

Think of an equation as a balance scale. Both sides weigh the same. If you do the same thing to both sides, it stays balanced.

Solve 3x + 2 = 14:

3x + 2 = 14
3x     = 12      subtract 2 from both sides
 x     = 4       divide both sides by 3

The trick is to undo the operations around x, in reverse order. x was multiplied by 3 and then had 2 added, so we subtract 2 first, then divide by 3.

Checking with substitution

Always check an answer by putting it back into the original equation:

Try it yourselfPython
x = 4
left = 3 * x + 2
right = 14
print(left, "==", right, "->", left == right)

Python can also search for a solution by trying values, which is a useful way to check your algebra:

Try it yourselfPython
for x in range(-10, 11):
    if 5 * x - 7 == 18:
        print("Solution found: x =", x)

Rearranging a formula

The same balancing steps let you rearrange a formula to find a different variable. To turn Fahrenheit back into Celsius:

F      = 1.8C + 32
F - 32 = 1.8C           subtract 32 from both sides
C      = (F - 32) / 1.8 divide both sides by 1.8
Try it yourselfPython
def to_fahrenheit(c):
    return 1.8 * c + 32


def to_celsius(f):
    return (f - 32) / 1.8


print(to_fahrenheit(100))
print(to_celsius(212))
print(round(to_celsius(to_fahrenheit(37)), 6))     # round trip returns 37

Converting there and back again gives the original value, which is a good test that the rearrangement is correct.

Two unknowns

Sometimes there are two unknowns and two equations, such as finding a line that passes through two points. You will do this in the Linear Functions lesson, and later Python libraries such as NumPy can solve thousands of equations at once.

Common mistakes

Practice exercise

Use a formula forwards, then rearrange it to work backwards.

Your solutionPython
# The formula for a taxi fare is: fare = 12 * km + 50
# 1. Write fare(km) to work out the fare.
# 2. Rearrange the formula to find km from a fare, and write km_for(fare_paid).
# 3. Print the fare for 10 km, and the distance for a fare of 290.

# Output must be exactly:
# Fare for 10 km: 170
# Distance for 290: 20.0 km
Knowledge check

Solve 4x - 3 = 17. What is x?

Summary

  • A variable stands for a number. An expression combines variables and numbers. An equation says two expressions are equal.
  • In 3x + 2, 3 is the coefficient and 2 is the constant.
  • Solve an equation by doing the same thing to both sides, undoing operations in reverse order.
  • Rearrange a formula the same way, and check answers by substitution.

Next: exponents and logarithms, two ideas that appear everywhere in AI.

Finished reading? Tick it off to track your progress.