Removing Parentheses
Parentheses group terms together, and before you can combine like terms across them you usually have to remove them. The rule is the distributive property: multiply everything inside by whatever is outside, paying special attention to a minus sign in front. This lesson covers single brackets, a minus sign, nested brackets and two brackets multiplied together.
A number or term in front
Multiply every term inside by the factor outside:
The most common mistake is to multiply only the first term: is not .
A plus or minus sign in front
- A plus sign in front: just drop the brackets. .
- A minus sign in front means multiplying by −1, so every sign inside flips:
Tip: Read a minus in front of a bracket as "−1 times". Then there is only one rule to remember, the distributive property, and the sign changes follow from it.
from sympy import symbols, expand
x, y = symbols("x y")
print(expand(10 - (x - 3)))
print(expand(5*y - (2*y + 7 - y)))
print(expand(-2*(5*y - 1)))
print(expand(3*(x + 4) - 2*(x - 5)))13 - x
4*y - 7
2 - 10*y
x + 22
The last line combines both steps: . Note that −2 times −5 is +10.
Nested brackets
When brackets sit inside other brackets, remove them from the inside out, simplifying as you go:
Working inside out keeps each step small; brackets in code are evaluated the same way.
Two brackets multiplied
To multiply , distribute each term of the first bracket over the whole second bracket. Every term meets every term, giving products:
A grid makes sure no product is forgotten, and it generalises to longer brackets:
first = [("x", 1), ("", 5)] # x + 5 as (variable part, coefficient)
second = [("x", 1), ("", -2)] # x - 2
for v1, c1 in first:
row = []
for v2, c2 in second:
mono = {"xx": "x²"}.get(v1 + v2, v1 + v2) # x·x is x²
row.append(f"{c1 * c2:+d}{mono}")
print(" ".join(row))+1x² -2x
+5x -10
The four cells are , , and , which combine to . The full method for longer polynomials is in Multiplying Polynomials.
In software
Removing brackets is what an optimiser does when it rewrites total - (discount - fee) as total - discount + fee, and it is where hand-written refactors go wrong. A bug that looks like price - discount + tax where the spec said price - (discount + tax) is exactly the "forgot to flip the sign" slip. When you change brackets in code, test the old and new expressions on a few values, as the exercise below does.
Exercises
Exercise 1: Distribute
Remove the parentheses and simplify: .
Answer
Solution
The −4 multiplies −5 to give +20.
Exercise 2: Minus in front
Simplify .
Answer
Solution
Exercise 3: Nested
Simplify .
Answer
Solution
from sympy import symbols, expand
x = symbols("x")
print(expand(3*(2*(x - 4) - (x + 1))))3*x - 27
Exercise 4: Two brackets
Expand .
Answer
Solution
Exercise 5: Test a refactor
A colleague rewrote price - (discount + tax) as price - discount + tax. Write code that compares both for price = 100, discount = 15, tax = 8, and give the correct removal of the brackets.
Answer
77 versus 93; the correct version is price - discount - tax.
Solution
The minus sign in front of the bracket flips both signs inside: .
price, discount, tax = 100, 15, 8
print(price - (discount + tax), price - discount + tax, price - discount - tax)77 93 77
Test yourself
Simplify .
Expand .
When brackets are nested, which do you remove first?
- The outermost
- The innermost
- Any order gives fewer steps
- The one with the minus sign