Sub topic 8
Nested parentheses and brackets
This chapter assumes you know the basic order of operations (exponents, then multiplication and division, then addition and subtraction), how to add and subtract negative integers, and the idea that parentheses come first. Here you will learn what to do when grouping symbols sit inside other grouping symbols.
Why nesting happens
A grouping symbol tells you to treat everything inside it as one value before using it. Real expressions often need a group inside a group. For example, you might subtract two numbers, use the result in a sum, and then multiply that sum by something.
When one group sits inside another, we say the groups are nested. The rule is short:
- Find the innermost group, the one that contains no other group.
- Evaluate it with the normal order of operations. Treat what is inside as its own small expression.
- Replace the group with its value.
- Repeat until no groups remain, then evaluate what is left.
Parentheses ( ), square brackets [ ], and braces { } mean the same thing in arithmetic. People alternate them only to make matching pairs easier to see. A common convention is parentheses innermost, then brackets, then braces. Rank does not depend on the shape of the symbol. It depends only on how deeply a group is nested.
A worked example
Evaluate 2 × [3 + (4 − 1) × 5] − 6.
Start with the innermost group, (4 − 1):
(4 − 1)is3, so the expression becomes2 × [3 + 3 × 5] − 6.- The bracket now holds
3 + 3 × 5. Inside it, multiplication comes before addition.3 × 5 = 15, so the bracket is3 + 15 = 18. - The expression is now
2 × 18 − 6. - Multiply:
36 − 6. - Subtract:
30.
The ordinary rules still apply inside every group. A bracket is not just "do the parentheses first". It is a complete expression that gets its own full pass through exponents, multiplication and division, and addition and subtraction.
What the outer symbols do after the inside is done
Once a group has been reduced to a single number, the symbols around it only mark that the number is a unit. Anything attached to the outside then applies to the whole value:
- An exponent outside a bracket applies to the bracket's final value.
- A minus sign in front of a bracket negates the final value.
- A number in front of a bracket multiplies the final value.
Take [6 − (2 − 5)]². The inner group (2 − 5) is −3. The bracket becomes 6 − (−3), which is 6 + 3 = 9. Then the exponent applies to 9, giving 81.
A very common slip is to drop the parentheses around a negative result and write 6 − 2 − 5. That gives −1, and squaring it gives 1, which is wrong. Subtracting a negative number adds, so keep the parentheses around the replaced value until you have applied the sign.
Tracking the layers
For long expressions, write one line per layer. Each line rewrites the whole expression with the innermost groups replaced by their values. This also helps you find mistakes later, because you can see which line went wrong.
| Line | Expression | What was done |
|---|---|---|
| 1 | `60 − 2 × [(7 − 4)² + 1]` | Starting expression |
| 2 | `60 − 2 × [3² + 1]` | Innermost: `7 − 4 = 3` |
| 3 | `60 − 2 × [9 + 1]` | Inside the bracket, exponent first: `3² = 9` |
| 4 | `60 − 2 × 10` | Bracket: `9 + 1 = 10` |
| 5 | `60 − 20` | Multiply: `2 × 10 = 20` |
| 6 | `40` | Subtract |
Notice that line 3 does the exponent before the addition inside the bracket. The order of operations is still in force inside each group.
Nesting with fractions
The same method works with fractions. Evaluate 1/2 + [3/4 − (1/3 + 1/6)]².
- Innermost group:
1/3 + 1/6 = 2/6 + 1/6 = 3/6 = 1/2. - The bracket becomes
3/4 − 1/2 = 3/4 − 2/4 = 1/4. - The exponent applies to the whole bracket value:
(1/4)² = 1/16. - Add:
1/2 + 1/16 = 8/16 + 1/16 = 9/16.
The exponent is not applied to each piece inside the bracket. (3/4 − 1/2)² is not 9/16 − 1/4. You must finish the subtraction first.
Checking nesting with Python
Python accepts only ( ) for grouping. In Python, [ ] makes a list and { } makes a set, so brackets and braces written by hand must be converted. The program below counts how deeply the groups nest, rejects unbalanced input, converts the symbols, and evaluates the result. It uses ^ for powers, as you would write by hand, and converts it to Python's **.
def max_depth(expr):
"""Return the deepest nesting level, or None if the groups do not match."""
depth = 0
deepest = 0
for ch in expr:
if ch in "([{":
depth += 1
deepest = max(deepest, depth)
elif ch in ")]}":
depth -= 1
if depth < 0:
return None
return deepest if depth == 0 else None
def evaluate(expr):
"""Evaluate a hand-written expression that uses ( ), [ ], { } and ^."""
python_expr = expr.translate(str.maketrans("[]{}", "()()")).replace("^", "**")
return eval(python_expr, {"__builtins__": {}})
tests = [
"2 * [3 + (4 - 1) * 5] - 6",
"60 - 2 * [(7 - 4) ^ 2 + 1]",
"3 * {2 + [4 - (1 + 1)] ^ 2}",
"[6 - (2 - 5)] ^ 2",
]
for text in tests:
print(f"{text} -> depth {max_depth(text)}, value {evaluate(text)}")The program prints the following:
2 * [3 + (4 - 1) * 5] - 6 -> depth 2, value 30
60 - 2 * [(7 - 4) ^ 2 + 1] -> depth 2, value 40
3 * {2 + [4 - (1 + 1)] ^ 2} -> depth 3, value 18
[6 - (2 - 5)] ^ 2 -> depth 2, value 81For exact fractions, use Fraction from the standard library:
from fractions import Fraction
result = Fraction(1, 2) + (Fraction(3, 4) - (Fraction(1, 3) + Fraction(1, 6))) ** 2
print(result) # 9/16Common errors with nested groups
- Starting at the left instead of the inside. The leftmost group is not necessarily the innermost. Look for a group that contains no other group.
- Ignoring the order of operations inside a group. In
[3 + (4 − 1) × 5], you must multiply before adding, even though you are inside a bracket. - Losing a sign when replacing a group. If a group evaluates to a negative number and a minus sign comes before it, keep the parentheses until you have applied the subtraction.
- Applying an outside exponent too early. The exponent acts on the finished value of the group, not on the individual terms.
- Mismatched symbols. Each opener needs a closer. If a bracket never closes, the expression is not well formed. The
max_depthfunction above returnsNonein that case.
Exercises
Exercise
Evaluate 4 × {10 − [2 + (9 − 6)²]}. Show each layer.
Show answer
Innermost: (9 − 6) = 3. Inside the bracket, the exponent comes first: 3² = 9, so [2 + 9] = 11. The braces hold 10 − 11 = −1. Finally 4 × (−1) = −4.
Exercise
Evaluate −[3 − (2 − 6)]². Is the minus sign squared?
Show answer
Innermost: (2 − 6) = −4. The bracket is 3 − (−4) = 7. The exponent applies to the bracket's value: 7² = 49. The leading minus sign is applied after the exponent, so the result is −49. The minus sign is not squared, because it sits outside the bracket and is not part of the base.