Which is easier to work with: 3x + 5 + 2x − 1 + x or 6x + 4? They're exactly the same expression. The second one is just simplified: the pieces that belong together have been combined. Simplifying makes formulas shorter, easier to calculate and easier to understand, and it's the first step in solving equations.
This lesson shows the three core moves for rearranging expressions: collecting like terms, expanding brackets and factorising (putting brackets back in). Each comes with step-by-step examples, a simple picture of why it works, and a Python check, so you can always confirm your answer.
What you need first: variables and expressions and negative numbers.
Key terms
- Like terms: terms with exactly the same variable part, like 3x and 5x, or 2x² and −x². Only the coefficient differs.
- Unlike terms: terms with different variable parts, like 3x and 3x², or 2a and 2b.
- Simplify: rewrite an expression in a shorter, equivalent form.
- Expand: remove brackets by multiplying everything inside them.
- Factorise: the reverse of expanding: rewrite an expression with brackets by taking out a common factor.
- Equivalent: giving the same value for every possible value of the variables.
Collecting like terms
Think of the variables as types of fruit: x is apples, y is bananas. 3 apples + 2 apples = 5 apples. But 3 apples + 2 bananas can't become "5 apple-bananas"; you can only write it as 3 apples + 2 bananas.
In algebra:
- 3x + 2x = 5x (like terms: add the coefficients)
- 7y − 4y = 3y
- 3x + 2y stays as it is (unlike terms)
- x² + x stays as it is: x² and x are unlike, just as square metres and metres are different things.
Step by step
Simplify 3x + 5 + 2x − 1 + x:
- Group the like terms, keeping each term's sign: (3x + 2x + x) + (5 − 1).
- Add the coefficients: x on its own has a coefficient of 1, so 3 + 2 + 1 = 6.
- Add the constants: 5 − 1 = 4.
- Result: 6x + 4.
Simplify 4a + 3b − a + 5b − 2:
- The a terms: 4a − a = 3a.
- The b terms: 3b + 5b = 8b.
- The constant: −2.
- Result: 3a + 8b − 2.
The sign belongs to the term after it. In 4a + 3b − a, the minus sign goes with the a, so a is subtracted. Keeping signs attached to their terms prevents most mistakes.
Multiplying terms
When terms are multiplied, they always combine, like or not:
- 3 × 4x = 12x
- 2x × 5x = 10x² (x × x = x²)
- 3a × 2b = 6ab
- 6x ÷ 2 = 3x
Expanding single brackets
3(x + 4) means three lots of (x + 4). Expanding means multiplying the number outside by every term inside:
3(x + 4) = 3 × x + 3 × 4 = 3x + 12
Why? Picture 3 bags, each holding x apples and 4 oranges. Altogether you have 3x apples and 12 oranges. This rule is called the distributive law.
More examples:
- 5(2y − 3) = 10y − 15
- −2(x + 6) = −2x − 12 (the minus is multiplied too)
- −(x − 7) = −x + 7 (a minus in front of brackets flips every sign inside)
- x(x + 3) = x² + 3x
- 4a(2a − b) = 8a² − 4ab
The most common mistake is multiplying only the first term: 3(x + 4) is not 3x + 4.
Expand, then simplify
Simplify 2(3x + 1) − 3(x − 4):
- Expand the first bracket: 6x + 2.
- Expand the second, carefully with the −3: −3x + 12.
- Combine: 6x + 2 − 3x + 12.
- Collect like terms: 3x + 14.
Expanding double brackets
(x + 3)(x + 5): every term in the first bracket multiplies every term in the second. That's four multiplications. A common way to remember them is FOIL:
| Step | Multiply | Result |
|---|---|---|
| First | x × x | x² |
| Outer | x × 5 | 5x |
| Inner | 3 × x | 3x |
| Last | 3 × 5 | 15 |
Add them and collect like terms: x² + 5x + 3x + 15 = x² + 8x + 15.
The area picture
Draw a rectangle with sides (x + 3) and (x + 5). Split each side into its two parts, and the rectangle splits into four smaller rectangles with areas x², 5x, 3x and 15. The total area is their sum. That's why every pair must be multiplied.
With negatives
(x − 2)(x + 7) = x² + 7x − 2x − 14 = x² + 5x − 14
Squaring a bracket
(x + 4)² means (x + 4)(x + 4), not x² + 16:
(x + 4)(x + 4) = x² + 4x + 4x + 16 = x² + 8x + 16
The middle term, 8x, is easy to forget. Check with numbers: if x = 1, (1 + 4)² = 25, and 1 + 8 + 16 = 25 ✓, while 1 + 16 = 17 ✗.
Two patterns worth knowing:
- (a + b)² = a² + 2ab + b²
- (a + b)(a − b) = a² − b² (the middle terms cancel)
Factorising: putting brackets back
Factorising is expanding in reverse. Find the biggest factor that divides every term, and take it outside the brackets:
- 6x + 9 = 3(2x + 3) (3 divides 6 and 9)
- 10a − 15 = 5(2a − 3)
- x² + 7x = x(x + 7) (x divides both terms)
- 4xy + 8x = 4x(y + 2)
Check by expanding: 3(2x + 3) = 6x + 9 ✓. Factorising is useful for simplifying fractions and, later, for solving quadratic equations.
Check your algebra with Python
Two expressions are equivalent if they give the same value for every input. You can't test every number, but testing several random values catches almost every mistake:
import random
def original(x):
return 2 * (3 * x + 1) - 3 * (x - 4)
def simplified(x):
return 3 * x + 14
def wrong(x):
return (x + 4) ** 2 - (x ** 2 + 16) # checks the "(x + 4)² = x² + 16" mistake
random.seed(1)
for x in [random.uniform(-10, 10) for _ in range(3)]:
print(round(original(x), 6), round(simplified(x), 6), round(wrong(x), 6))
-7.938145 -7.938145 -58.501721
34.846024 34.846024 55.589398
29.826477 29.826477 42.203939
The first two columns always match, so the simplification is correct. The third column would be 0 every time if (x + 4)² really equalled x² + 16; it isn't, so that "simplification" is wrong. (It equals 8x, the forgotten middle term.)
For full symbolic algebra in Python, there's a library called SymPy, which can expand and factorise expressions exactly. This course checks with numbers instead, which needs no extra installation and works for any formula.
Why simplifying matters in data science
- Faster calculations: an expression with fewer operations runs faster when applied to millions of rows.
- Understanding models: expanding a model's formula shows what each feature really contributes. Polynomial regression expands (x₁ + x₂)² into x₁², 2x₁x₂ and x₂², creating new interaction features such as x₁x₂.
- Deriving formulas: almost every formula in statistics and ML is derived by expanding and collecting terms, and you'll see that in the Statistics and Calculus courses.
Practice
- Simplify: (a) 5x + 3 − 2x + 7 (b) 4a − 3b + 2a + b (c) 3x² + 2x − x² + 5x
Show the answer
(a) 3x + 10 (b) 6a − 2b (c) 2x² + 7x
- Expand: (a) 4(2x − 5) (b) −3(y + 2) (c) 2x(x − 6)
Show the answer
(a) 8x − 20 (b) −3y − 6 (c) 2x² − 12x
- Expand and simplify (x + 6)(x − 2).
Show the answer
x² − 2x + 6x − 12 = x² + 4x − 12.
- Expand (x − 3)².
Show the answer
(x − 3)(x − 3) = x² − 3x − 3x + 9 = x² − 6x + 9.
- Factorise: (a) 12x + 18 (b) x² − 5x (c) 6ab + 9a
Show the answer
(a) 6(2x + 3) (b) x(x − 5) (c) 3a(2b + 3)
Key takeaways
- Only like terms combine: 3x + 2x = 5x, but 3x + 2y and x² + x stay as they are.
- Every sign belongs to the term after it.
- Expanding multiplies the outside by every term inside; double brackets need all four products.
- (x + 4)² is x² + 8x + 16, never x² + 16.
- Factorising takes out a common factor; check it by expanding.
- Test with a few numbers to check any simplification.
Frequently asked questions
What are like terms in algebra?
Terms with exactly the same variables raised to the same powers, such as 4x and −7x, or 3xy and xy. Only like terms can be added or subtracted into a single term.
What does FOIL mean?
First, Outer, Inner, Last: the four products you need when multiplying two brackets with two terms each. It's a reminder to multiply every term in one bracket by every term in the other.
What is the difference between expanding and factorising?
Expanding removes brackets by multiplying out: 3(x + 2) becomes 3x + 6. Factorising does the reverse, putting a common factor outside brackets: 3x + 6 becomes 3(x + 2).
What to learn next
- Use these skills to find unknowns in solve linear equations step by step.
- Revisit variables and expressions.