Sub topic 6
Grouping symbols come first
This chapter assumes you can add, subtract, multiply and divide signed integers. It also assumes you know that multiplication and division rank above addition and subtraction, and that an exponent is repeated multiplication. Here you will learn how parentheses and brackets override that default ranking. Nested groups and the fraction bar get their own chapters.
What a grouping symbol does
A grouping symbol wraps part of an expression so it is treated as a single number. The common ones are parentheses ( ), square brackets [ ] and braces { }. In arithmetic they all mean the same thing: work out what is inside first, then use the result as one value.
Without grouping, multiplication happens before addition:
print(2 + 3 * 4) # 14: 3 * 4 = 12, then 2 + 12
print((2 + 3) * 4) # 20: 2 + 3 = 5, then 5 * 4The numbers and operators are identical in both lines. Only the grouping differs, and so does the answer. Grouping is how you tell a reader, or a computer, which operation to do early.
The procedure
For any group, follow these steps:
- Find a group that contains no other group.
- Evaluate its contents using the usual order: exponents, then multiplication and division left to right, then addition and subtraction left to right.
- Replace the whole group, symbols included, with the single value you found.
- Continue with the rest of the expression.
Step 3 is where most slips happen. Replace the group with one value and carry the sign along. Do not leave pieces of it behind.
Worked example with a negative result
Evaluate 6 * (4 - 7) + 10.
- The group is
(4 - 7). Its value is-3. - Replace it:
6 * (-3) + 10. The parentheses around-3only keep the sign from colliding with the*. - Multiply:
-18 + 10. - Add:
-8.
print(6 * (4 - 7) + 10) # -8Grouping changes the answer: a comparison
The table below pairs expressions that differ only by parentheses.
| Without grouping | Value | With grouping | Value |
|---|---|---|---|
| `20 / 2 + 3` | 13 | `20 / (2 + 3)` | 4 |
| `2 + 3 ** 2` | 11 | `(2 + 3) ** 2` | 25 |
| `10 - 3 + 4` | 11 | `10 - (3 + 4)` | 3 |
| `5 * 2 - 6` | 4 | `5 * (2 - 6)` | -20 |
print(20 / 2 + 3, 20 / (2 + 3)) # 13.0 4.0
print(2 + 3 ** 2, (2 + 3) ** 2) # 11 25
print(10 - 3 + 4, 10 - (3 + 4)) # 11 3
print(5 * 2 - 6, 5 * (2 - 6)) # 4 -20Python's / always returns a decimal number, which is why the first line prints 13.0 and 4.0 instead of 13 and 4.
A minus sign in front of a group
The third row of the table is the most common source of error. A group is one value, so a minus sign before it applies to that whole value:
10 - (3 + 4) becomes 10 - 7, which is 3.
Writing 10 - 3 + 4 instead is a different expression, because subtraction then binds only to the 3. A sign before a group also flips every term inside it if you remove the parentheses:
10 - (3 + 4) = 10 - 3 - 4 = 3
Subtracting a negative inside the group works the same way. For -(3 - 8), evaluate the group first to get -5. The minus sign then turns it into 5.
print(-(3 - 8)) # 5
print(10 - (3 - 8)) # 15: the group is -5, and 10 - (-5) = 15
print(10 - 3 - 8) # -1: different expression, different valueGrouping and multiplication
A number written directly beside a group multiplies the group's value. The same rule gives the distributive property, where multiplying a sum equals multiplying each term:
3 * (4 + 5) = 3 * 9 = 27, and 3 * 4 + 3 * 5 = 12 + 15 = 27.
Both routes agree, so you can choose whichever is easier. If the group contains a variable you cannot simplify, distributing is the only option. For plain numbers, evaluating the group first is usually shorter.
print(3 * (4 + 5)) # 27
print(3 * 4 + 3 * 5) # 27Writing 3(4 + 5) without a * is fine on paper. Python rejects it as a syntax error, so you must type the *. The chapter on ambiguous notation covers cases where implicit multiplication causes trouble.
Grouping and fractions
Fractions follow the same rule. Evaluate (1/2 + 1/3) * 6:
- The group is
1/2 + 1/3. A common denominator of 6 gives3/6 + 2/6 = 5/6. - Multiply:
5/6 * 6 = 5.
Remove the parentheses and the value changes. Multiplication now happens before addition, so 1/2 + 1/3 * 6 is 1/2 + 2 = 5/2. The Fraction class gives exact results to compare with a hand calculation:
from fractions import Fraction
grouped = (Fraction(1, 2) + Fraction(1, 3)) * 6
ungrouped = Fraction(1, 2) + Fraction(1, 3) * 6
print(grouped) # 5
print(ungrouped) # 5/2Brackets and braces
On paper, [ ] and { } mean exactly what ( ) means. People mostly use them to make nested groups easier to see. You will meet them in the next chapter on nesting.
In Python they mean something else. Square brackets build lists and braces build sets or dictionaries. For arithmetic, use only parentheses, even for several levels:
print((8 - 2) * (3 + 1)) # 24: two separate groups, each evaluated firstWhen to add parentheses that are not required
Parentheses that do not change the value are allowed and can help. (3 * 4) + 2 equals 3 * 4 + 2. Adding them can make your intent clear to a reader. Extra parentheses never change the answer, but missing ones can. When you transcribe a written expression into code, add parentheses wherever you are unsure how it will be read.
Common slips with groups
- Doing a multiplication that sits outside the group before finishing the group. In
2 * (3 + 4) - 5, the sum comes first, so the result is2 * 7 - 5 = 9. - Dropping the parentheses when a negative value is substituted.
5 - (-2)is7, but writing5 - -2or5 - 2gives other results. - Treating
10 - (3 + 4)as10 - 3 + 4. - Evaluating the group correctly but then adding the numbers outside it first. Outside the group the usual order still applies.
Exercise
Evaluate each expression by hand, then check with Python: (a) 7 - (2 + 3) * 4, (b) (10 - 4) / (1 + 2), (c) 15 - (6 - 9).
Show answer
(a) The group is 2 + 3 = 5, giving 7 - 5 * 4. Multiplication comes next: 7 - 20 = -13. (b) The groups are 6 and 3, so 6 / 3 = 2 (Python prints 2.0). (c) The group is 6 - 9 = -3, giving 15 - (-3) = 18.
Exercise
Write a Python expression for "the sum of 1/2 and 1/3, multiplied by 6", using Fraction. Then explain what value you get if the parentheses are left out.
Show answer
print((Fraction(1, 2) + Fraction(1, 3)) 6) gives 5. Without the parentheses, Fraction(1, 3) 6 is evaluated first and equals 2, so the result is 1/2 + 2 = 5/2.