Skip to main content

Expressions and the rules of algebra

A letter stands for a number. Evaluate expressions such as wx + b in the agreed order, read powers and square roots, and see the commutative, associative and distributive laws as rectangles of area, ending with (a + b) squared read off a cut-up square.
M01.1.2L0 Foundation25 minIn review

Before this lesson

M01.1.1 Kinds of numbers and the number line

After this lesson you can

  • Evaluate an expression with brackets, powers, products and sums in the agreed order of operations.
  • Read a whole-number power as repeated multiplication, and a square root as the same question asked backwards.
  • Use the commutative, associative and distributive laws to expand and to factor, and explain each law with a rectangle of area.
  • Expand (a + b) squared into a squared + 2ab + b squared, and explain from a cut-up square where the middle term comes from.

In review. The maths and code on this page are checked by automated tests on every change, but an independent reader has not signed it off yet. Spot a mistake or a confusing step? Tell us.

The question

A model predicts with the formula \({wx + b}\). With \({w = 1.4}\), \({b = 0.5}\) and the input \({x = 3}\), is its prediction 4.7 or 4.9? And why is everyone who writes \({(a + b)^2 = a^2 + b^2}\) wrong by exactly \(2ab\)?

From the last lesson

1. Write 3/4 as a decimal and as a percentage.

Show answer

0.75 and 75%. Divide 3 by 4 to get 0.75, then multiply by 100.

2. What is \({\abs{-7}}\), and what does it measure?

Show answer
  1. It is the distance from \(-7\) to 0 on the number line.

Intuition

In algebra a letter stands for a number: one we do not know yet, or one that changes from case to case. A model’s formula \({wx + b}\) is a recipe. Give it numbers for \(w\), \(x\) and \(b\) and it gives back a number.

The rules for working with letters are not new facts. They are facts about numbers that you already use, written once so that they hold for every number. Many of them can be seen, as areas.

Two panels. Left: a rectangle of unit squares, 3 rows high and 6 columns wide, with a green dashed cut after the fourth column. The left part, 3 by 4, is tinted blue and labelled 3 times 4 equals 12; the right part, 3 by 2, is tinted sky blue and labelled 3 times 2 equals 6. The title reads 3 times (4 plus 2) equals 3 times 4 plus 3 times 2, and the note says 18 squares, counted either way. Right: the same picture with letters, a rectangle of height a cut into widths b and c, holding the areas ab and ac, under the title a times (b plus c) equals ab plus ac.
M01.1.2-F1

One rectangle, two ways to count its area. Count it whole, \({3 \times 6 = 18}\), or count the two pieces, \({12 + 6 = 18}\). The same cut works for any lengths: that is the distributive law.

The left picture is about the numbers 3, 4 and 2. The right picture is the same picture with letters, and it says something about every three numbers at once. Turning a picture like this into a rule is what algebra is for.

Build it up

Step 1. Letters next to each other multiply. \(wx\) means \(w\) times \(x\). Between two numbers we write the times sign, \({1.4 \times 3}\), so that 1.4 and 3 do not run together. An expression is any such recipe of numbers, letters and operations. To evaluate it, put numbers in place of the letters and work it out.

Step 2. The agreed order. Everyone works an expression out in the same order:

  1. brackets first;
  2. then powers;
  3. then multiplication and division, from left to right;
  4. then addition and subtraction, from left to right.

So \({2 + 3 \times 4}\) is 14, because the multiplication goes first, while \({(2 + 3) \times 4}\) is 20. Left to right matters too: \({10 - 4 - 3}\) is \({6 - 3 = 3}\), not \({10 - 1 = 9}\). In the question above, \({1.4 \times 3 + 0.5}\) is \({4.2 + 0.5 = 4.7}\). The answer 4.9 would need brackets round \({3 + 0.5}\).

Step 3. Powers count the factors. \(x^2\), read “\(x\) squared”, means \({x \times x}\). \(x^3\), “\(x\) cubed”, means \({x \times x \times x}\). In \(2^5\) the small raised 5 counts the factors: five 2s multiplied together, which gives 32. A power touches only what it is attached to: \({3 \times 2^2}\) is \({3 \times 4 = 12}\), and \(-3^2\) is \({-(3 \times 3) = -9}\). Write \({(-3)^2}\) for 9.

Step 4. A square root asks the question backwards. \(\sqrt{25}\) asks: which number, squared, gives 25? The answer is 5, since \({5 \times 5 = 25}\). Now \(-5\) squared is 25 as well, but the sign \(\sqrt{\phantom{x}}\) always means the answer that is not negative. Lesson M01.1.1 met \(\sqrt{2}\), the side of a square of area 2.

Step 5. Order and grouping do not matter.

Two panels. Left, titled order does not matter: a grid of 3 rows of 5 squares, with the top row tinted blue and labelled a row of 5 and the last column tinted sky blue and labelled a column of 3; underneath, 3 rows of 5 equals 5 columns of 3, and 3 times 5 equals 5 times 3 equals 15. Right, titled grouping does not matter: a 4 by 6 grid cut by a green dashed line down the middle and a green dotted line across the middle into four 2 by 3 blocks; underneath, two halves of 3 times 4 equals 12, or four blocks of 2 times 3 equals 6, and 2 times (3 times 4) equals (2 times 3) times 4 equals 24.
M01.1.2-F2

Left: count by rows or by columns, the total is the same. Right: count in two halves of 12 or in four blocks of 6, the total is the same.

Reading a rectangle by rows or by columns is the commutative law, \({ab = ba}\). Grouping its squares in halves or in quarters is the associative law, \({(ab)c = a(bc)}\). Adding works the same way: \({a + b = b + a}\), and \({(a + b) + c = a + (b + c)}\).

Step 6. A square cut in four. A square of side \({a + b}\) cut along the two lengths gives two squares and two equal rectangles:

Two panels. Left: a 5 by 5 square of unit squares, with sides marked 3 and 2 along the top and down the left. It is cut into a 3 by 3 square holding 9, a 2 by 2 square holding 4, both tinted blue, and two 3 by 2 rectangles holding 6 each, tinted sky blue. The title reads (3 plus 2) squared equals 9 plus 6 plus 6 plus 4 equals 25. Right: the same cut with letters, sides a and b, pieces a squared, ab, ab and b squared, under the title (a plus b) squared equals a squared plus 2ab plus b squared.
M01.1.2-F3

The square on \({a + b}\) is made of \(a^2\), \(b^2\) and two rectangles \(ab\): \({(a + b)^2 = a^2 + 2ab + b^2}\).

The formal statement

NoteDefinitions: powers and square roots

For a natural number \(n\), the power \(b^n\) is \(n\) factors of \(b\) multiplied together, so \({b^1 = b}\) and \({b^2 = b \times b}\). For a number \(a\) that is not negative, the square root \(\sqrt{a}\) is the number that is not negative and whose square is \(a\).

NoteThe laws of algebra

For all numbers \(a\), \(b\) and \(c\):

  • Commutative: \({a + b = b + a}\) and \({ab = ba}\).
  • Associative: \({(a + b) + c = a + (b + c)}\) and \({(ab)c = a(bc)}\).
  • Distributive: \({a(b + c) = ab + ac}\).

The pictures show positive lengths, but the laws hold for every number, negatives included; they are the ground rules that every later lesson builds on. Using the distributive law from left to right is expanding: \({3(x + 4) = 3x + 12}\). Using it from right to left is factoring: in \({6x + 9}\) both terms are 3 times something, so \({6x + 9 = 3(2x + 3)}\).

The square of a sum, step by step. The picture of Step 6 says what the answer is. The laws say why, with nothing skipped:

\[ \begin{aligned} (a + b)^2 &= (a + b)(a + b) \\ &= (a + b)\,a + (a + b)\,b \\ &= a(a + b) + b(a + b) \\ &= a^2 + ab + ba + b^2 \\ &= a^2 + 2ab + b^2 . \end{aligned} \]

Line 1 is what a square means. Line 2 is the distributive law, with the whole of \({(a + b)}\) playing the part of the single factor in front. Line 3 swaps each product round, by the commutative law. Line 4 uses the distributive law twice more, with \({a \times a = a^2}\) and \({b \times b = b^2}\). Line 5 uses \({ba = ab}\), so that the middle two terms are two copies of \(ab\).

Worked example

The model \({wx + b}\) has \({w = 1.4}\) and \({b = 0.5}\). For the input \({x = 3}\) the true answer is \({y = 5}\). Work out the squared error \({(wx + b - y)^2}\).

Machine learning scores a prediction this way: take the error, prediction minus truth, and square it, so that a miss counts the same above or below. The four points of this course have the inputs 1, 2, 3, 4, and this is the third of them.

A trace in five lines. The heading reads squared error (w x plus b minus y) squared with w equals 1.4, b equals 0.5 in orange, x equals 3 in blue and y equals 5. Line 1: (1.4 times 3 plus 0.5 minus 5) squared, with 1.4 times 3 highlighted in yellow and the note brackets first; inside them, multiply first. Line 2: equals (4.2 plus 0.5 minus 5) squared, with 4.2 plus 0.5 highlighted and the note then add and subtract, from left to right. Line 3: equals (4.7 minus 5) squared, with 4.7 minus 5 highlighted and the note finish the bracket: 4.7 minus 5 equals minus 0.3. Line 4: equals (minus 0.3) squared, highlighted, with the note the power last: minus 0.3 times minus 0.3. Line 5: equals 0.09, in vermillion, with the note a square is never negative.
M01.1.2-F4

The squared error worked out in the agreed order. The highlight marks the part that is worked out next, and the note on the right gives the rule.

  1. Inside the bracket, multiplication comes first: \({1.4 \times 3 = 4.2}\).
  2. Then the additions and subtractions, from left to right: \({4.2 + 0.5 = 4.7}\), the prediction.
  3. Then \({4.7 - 5 = -0.3}\), the error. The bracket now holds one number.
  4. Last, the power: \({(-0.3)^2}\) means \({(-0.3) \times (-0.3)}\), which is \(0.09\), because a negative times a negative is positive.

The squared error is 0.09.

Expand and check. Expand \({(x + 3)^2}\) with the rule of Step 6, taking \({a = x}\) and \({b = 3}\):

\[ \begin{aligned} (x + 3)^2 &= x^2 + 2 \times 3x + 9 \\ &= x^2 + 6x + 9 . \end{aligned} \]

Check at \({x = 2}\). The left side is \({(2 + 3)^2 = 25}\). The right side is \({4 + 12 + 9 = 25}\). They agree. A check like this catches slips, but one value proves nothing; the laws do the proving.

Code

First the model’s formula, one operation per line, then in one line.

# the weight, a number that shapes the rule
w = 1.4
# the bias, another such number
b = 0.5
# the input
x = 3
# multiplication comes first
product = w * x
# then the addition
prediction = product + b
print(product, prediction)
# rounded to 4 decimal places
print(round(prediction, 4))
# one line: Python also multiplies before it adds
print(round(w * x + b, 4))
4.199999999999999 4.699999999999999
4.7
4.7

Python printed 4.199999999999999 and 4.699999999999999. The decimal 1.4 is stored only approximately, as lesson M01.1.1 explained, and the tiny error shows in the last digit. Rounded to 4 decimal places, the answers are the 4.2 and 4.7 of Step 2. Now the squared error, and a warning about the minus sign:

# the true answer for this input
y = 5
# the model's mistake
error = prediction - y
print(round(error, 4), round(error ** 2, 4))
# ** goes before the minus sign, as in maths
print(-0.3 ** 2, (-0.3) ** 2)
-0.3 0.09
-0.09 0.09

Next, a loop that tests the square of a sum on every pair of whole numbers from \(-10\) to 10. Whole numbers are stored exactly, so == is safe here.

pairs = 0
failures = 0
# 21 values of a
for a in range(-10, 11):
    # and 21 values of the second number, c: 441 pairs
    for c in range(-10, 11):
        pairs = pairs + 1
        if (a + c) ** 2 != a ** 2 + 2 * a * c + c ** 2:
            failures = failures + 1
print(pairs, "pairs tried,", failures, "failures")

held = 0
for a in range(-10, 11):
    for c in range(-10, 11):
        # the wrong rule
        if (a + c) ** 2 == a ** 2 + c ** 2:
            held = held + 1
print("the wrong rule held for", held, "pairs")
441 pairs tried, 0 failures
the wrong rule held for 41 pairs

The true rule passed all 441 tests. That is evidence, not proof: the proof is the step-by-step derivation above, which covers every number at once. The wrong rule held for 41 pairs, exactly those where \(a\) or \(c\) is 0, so that the missing \(2ac\) happens to be 0. Last, a square root:

# the standard maths module
import math
print(math.sqrt(25), math.sqrt(0.09))
# a decimal close to the square root of 2
root = math.sqrt(2)
print(root, root * root)
5.0 0.3
1.4142135623730951 2.0000000000000004

math.sqrt(2) is a stored decimal, which is a fraction, so by lesson M01.1.1 it cannot be exactly \(\sqrt{2}\): its square comes out as 2.0000000000000004.

Explore

This module’s interactive, the equation balance, lives in lesson M01.1.3. There you will use the laws of this lesson to undo an expression such as \({3x + 2}\), one operation at a time.

Pitfalls

Two panels showing the same 5 by 5 square with sides 3 and 2. The left panel, marked with a cross and titled (3 plus 2) squared equals 3 squared plus 2 squared, question mark, fills only the 3 by 3 square, holding 9, and the 2 by 2 square, holding 4; the two 3 by 2 rectangles are hatched in purple and marked missing, and the note says 9 plus 4 equals 13, but the square holds 25. The right panel, marked with a tick and titled (3 plus 2) squared equals 3 squared plus 2 times 3 times 2 plus 2 squared, fills all four pieces, 9, 6, 6 and 4, with the note 9 plus 6 plus 6 plus 4 equals 25.
M01.1.2-F5

Wrong beside right. Squaring each part leaves out the two rectangles, which together are \(2ab\).

“\({(a + b)^2 = a^2 + b^2}\).”

The picture shows two rectangles missing. With \({a = 3}\) and \({b = 2}\) the wrong rule gives 13 instead of 25. The same slip in machine learning is to treat the squared error \({(\hat{y} - y)^2}\), where \(\hat{y}\) is the prediction, as if it were \({\hat{y}^2 - y^2}\). It is not.

“\(-3^2\) is 9.”

The power goes first and touches only the 3: \({-3^2 = -(3^2) = -9}\). Python agrees, so -3 ** 2 gives -9. Write \({(-3)^2}\), or (-3) ** 2 in code, when you mean 9.

“\({2 + 3 \times 4}\) is 20.”

Multiplication comes before addition: \({2 + 12 = 14}\). Brackets are how you ask for the other order, \({(2 + 3) \times 4 = 20}\). When in doubt, put brackets round the part that goes first: brackets that follow the agreed order change nothing, and they make the order plain.

“\(\sqrt{25}\) is 5 or \(-5\).”

Both 5 and \(-5\) square to 25, but the sign \(\sqrt{\phantom{x}}\) names only the one that is not negative: \({\sqrt{25} = 5}\). When both are wanted, they are written \({\pm 5}\), read “plus or minus 5”.

Check yourself

Recall

1. In what order is \({3 + 4 \times 2^2}\) worked out, and what is its value?

Show answer

The power first, \({2^2 = 4}\); then the multiplication, \({4 \times 4 = 16}\); then the addition, \({3 + 16 = 19}\).

2. What does \(\sqrt{49}\) mean, and what is it?

Show answer

The number that is not negative and whose square is 49. It is 7.

3. State the distributive law, and say how a rectangle shows it.

Show answer

\({a(b + c) = ab + ac}\). A rectangle of height \(a\) and width \({b + c}\) has area \({a(b + c)}\); cutting it into widths \(b\) and \(c\) gives two pieces of areas \(ab\) and \(ac\).

4. Expand \({(a + b)^2}\).

Show answer

\({a^2 + 2ab + b^2}\): two squares and two rectangles \(ab\).

5. A loop checks an identity on 441 pairs of numbers and finds no failure. Why is that evidence but not proof?

Show answer

An identity claims something for every number, and 441 pairs are not every number. Only an argument with letters, or a picture that works for any lengths, covers them all.

Exercises

1. ★ Take \({w = 2}\), \({x = -3}\) and \({b = 7}\). Evaluate \({wx + b}\), then \({(wx + b)^2}\), then \({wx^2 + b}\).

Show solution

\[ \begin{aligned} wx + b &= 2 \times (-3) + 7 \\ &= -6 + 7 = 1 . \end{aligned} \]

Then \({(wx + b)^2 = 1^2 = 1}\): the bracket is worked out first.

In \({wx^2 + b}\) the power touches only \(x\), and \(x^2\) is \({(-3) \times (-3)}\), which is 9. So

\[ \begin{aligned} wx^2 + b &= 2 \times 9 + 7 \\ &= 18 + 7 = 25 . \end{aligned} \]

2. ★★ Expand \({3(x + 4)}\) and \({(x + 5)^2}\), and factor \({12x + 18}\). Check each answer at \({x = 2}\).

Show solution

\({3(x + 4) = 3x + 12}\). At \({x = 2}\): \({3 \times 6 = 18}\) and \({6 + 12 = 18}\).

\[ \begin{aligned} (x + 5)^2 &= x^2 + 2 \times 5x + 25 \\ &= x^2 + 10x + 25 . \end{aligned} \]

At \({x = 2}\): \({7^2 = 49}\) and \({4 + 20 + 25 = 49}\).

\({12x + 18}\): both terms are 6 times something, \({12x = 6 \times 2x}\) and \({18 = 6 \times 3}\), so \({12x + 18 = 6(2x + 3)}\). At \({x = 2}\): \({24 + 18 = 42}\) and \({6 \times 7 = 42}\).

3. ★★★ Use the laws, step by step, to show that \({(a + b)(a - b) = a^2 - b^2}\). Then use it to work out \({31 \times 29}\) in your head.

Show solution

Treat \({(a + b)}\) as one factor and use the distributive law on \({(a - b)}\), then the commutative and distributive laws again:

\[ \begin{aligned} & (a + b)(a - b) \\ &\quad = (a + b)\,a - (a + b)\,b \\ &\quad = a(a + b) - b(a + b) \\ &\quad = a^2 + ab - (ba + b^2) \\ &\quad = a^2 + ab - ab - b^2 \\ &\quad = a^2 - b^2 . \end{aligned} \]

The step from \({a^2 + ab - (ba + b^2)}\) to the line below it takes away each term of the bracket, and writes \(ba\) as \(ab\) by the commutative law. Then \({ab - ab = 0}\).

Now take \({a = 30}\) and \({b = 1}\):

\[ \begin{aligned} 31 \times 29 &= (30 + 1)(30 - 1) \\ &= 30^2 - 1^2 \\ &= 900 - 1 = 899 . \end{aligned} \]

Summary card

The rules of algebra are facts about numbers, and many can be seen as areas.

The anchor figure again: a 3 by 6 rectangle of unit squares cut into a 3 by 4 piece and a 3 by 2 piece, beside the same rectangle with letters, a times (b plus c) cut into ab and ac.
M01.1.2-F1

One rectangle, two counts.

  • Order: brackets, then powers, then \({\times}\) and \({\div}\), then \({+}\) and \(-\), each left to right.
  • \(b^n\) multiplies \(n\) factors \(b\); \(\sqrt{a}\) is the number, not negative, that squares to \(a\).
  • \({ab = ba}\), \({(ab)c = a(bc)}\) and \({a(b + c) = ab + ac}\), and the same for sums.
  • \({(a + b)^2 = a^2 + 2ab + b^2}\).

Sources and next steps

Sources. Each was opened and checked against this lesson on 12 November 2026.

  • Jay Abramson and others, Algebra and Trigonometry 2e, OpenStax. Section 1.1 gives the order of operations (brackets, exponents, multiplication and division, addition and subtraction) and the commutative, associative and distributive properties. Section 1.3 defines the principal square root as the number, not negative, whose square is \(a\), and answers “no” to “does \({\sqrt{25} = \pm 5}\)?”. Section 1.4 states that a squared binomial is the first term squared, plus double the product of the two terms, plus the last term squared.
  • Python Software Foundation, An Informal Introduction to Python, The Python Tutorial, Section 3.1.1 (Python 3.12). Its footnote says that because ** has higher precedence than -, -3**2 means -(3**2) and gives -9. The Python Language Reference, Section 6.5, The power operator, gives the rule and the example -1**2, which is -1.

Next. M01.1.3 Equations and inequalities runs these operations backwards to solve for an unknown, on a balance.

This lesson unlocks

M01.1.3 Equations and inequalities

Your progress is saved in this browser only. No account, no tracking.
Back to top