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.
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
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.
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:
brackets first;
then powers;
then multiplication and division, from left to right;
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.
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:
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.
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.
Inside the bracket, multiplication comes first: \({1.4 \times 3 = 4.2}\).
Then the additions and subtractions, from left to right: \({4.2 + 0.5 = 4.7}\), the prediction.
Then \({4.7 - 5 = -0.3}\), the error. The bracket now holds one number.
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}\):
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 rulew =1.4# the bias, another such numberb =0.5# the inputx =3# multiplication comes firstproduct = w * x# then the additionprediction = product + bprint(product, prediction)# rounded to 4 decimal placesprint(round(prediction, 4))# one line: Python also multiplies before it addsprint(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 inputy =5# the model's mistakeerror = prediction - yprint(round(error, 4), round(error **2, 4))# ** goes before the minus sign, as in mathsprint(-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 =0failures =0# 21 values of afor a inrange(-10, 11):# and 21 values of the second number, c: 441 pairsfor c inrange(-10, 11): pairs = pairs +1if (a + c) **2!= a **2+2* a * c + c **2: failures = failures +1print(pairs, "pairs tried,", failures, "failures")held =0for a inrange(-10, 11):for c inrange(-10, 11):# the wrong ruleif (a + c) **2== a **2+ c **2: held = held +1print("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 moduleimport mathprint(math.sqrt(25), math.sqrt(0.09))# a decimal close to the square root of 2root = 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
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}\).
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}\).
The rules of algebra are facts about numbers, and many can be seen as areas.
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.