Sub topic 11
The fraction bar groups the top and the bottom
This chapter assumes you can add, subtract, multiply and divide whole numbers, work with negative integers, and read a fraction as a numerator over a denominator. It also assumes you know the earlier levels of the order of operations: grouping, exponents, multiplication and division, then addition and subtraction.
The bar is a grouping symbol
A fraction bar does two jobs. It means "divide", and it also groups everything above it and everything below it. Take this fraction:
(3 + 5) / (2 + 2)
You never see parentheses around the top or bottom when it is written with a bar. The bar's length shows where each part starts and stops. So the numerator is evaluated as its own expression, the denominator is evaluated as its own expression, and the division happens last.
That makes the bar a grouping symbol, like parentheses. It sits at the first level of the order of operations, before exponents.
Evaluating a fraction step by step
Work on the top and bottom separately, then divide. Take this expression:
(5 + 3 · 2²) / (10 − 4 · 2)
- Numerator: the exponent comes first, so 2² = 4. Then 3 · 4 = 12. Then 5 + 12 = 17.
- Denominator: multiplication comes first, so 4 · 2 = 8. Then 10 − 8 = 2.
- Divide: 17 / 2 = 8.5.
Inside each part, the usual rules apply in full. The bar only decides where each part begins and ends.
Typing a fraction on one line
Code and most calculators have no bar. They have a slash, which only divides the number or group directly next to it. When you type a fraction, you must add the parentheses that the bar implied.
print((3 + 5) / (2 + 2)) # 2.0: the numerator and denominator are each grouped
print(3 + 5 / 2 + 2) # 7.5: without grouping, only the 5 is divided by 2The second line is a different expression. Division ranks above addition, so 5 / 2 is evaluated first, and the 3 and the 2 are simply added to the result.
The same habit works for the longer example:
print((5 + 3 * 2 ** 2) / (10 - 4 * 2)) # 8.5The parentheses go around each whole part. Inside them, 2 ** 2 still happens before 3 *, and 3 * ... still happens before 5 +.
Negative numbers in the top or bottom
Signs belong to the part they are written in. Evaluate each part to a single signed number, then divide.
print((-4 - 2) / (1 - 4)) # 2.0: -6 divided by -3
print(-4 - 2 / 1 - 4) # -10.0: no grouping, so only 2 is divided by 1A negative divided by a negative is positive, so the first result is 2. Dropping the parentheses changes the meaning completely.
Exponents inside a part follow the rules from the previous chapter. A leading minus sign is applied after the exponent unless parentheses make the negative number the base.
print((2 ** 3 - 4) / (2 * 3 - 2)) # 1.0: (8 - 4) / (6 - 2)
print((7 - 3 ** 2) / (2 - 5 + 1)) # 1.0: (7 - 9) / (-2), so -2 / -2Fractions inside fractions
A fraction can appear in the numerator or denominator. Treat the inner fractions as part of the expression you are evaluating. Use exact arithmetic when you check it, so you do not need to read rounded decimals.
from fractions import Fraction
top = Fraction(1, 2) + Fraction(1, 3) # 5/6
bottom = Fraction(1, 4)
print(top / bottom) # 10/3By hand: 1/2 + 1/3 = 3/6 + 2/6 = 5/6. Dividing by 1/4 means multiplying by 4, so 5/6 · 4 = 20/6 = 10/3.
A long bar that separates a stack of fractions is the main bar. When two bars of different lengths appear, the longer one divides last. In text form, put parentheses around the whole numerator and denominator so the main division is obvious.
The slash is not a bar
A chain of slashes is evaluated left to right, because division is at the same rank as multiplication.
print(1 / 2 / 4) # 0.125: (1/2)/4
print(1 / (2 / 4)) # 2.0: the grouping changes the orderA written fraction with stacked bars has only one reading. A one-line 1/2/4 is ambiguous to readers even though Python has a fixed rule. When you write math for other people, add parentheses.
The same care applies to a fraction raised to a power. The bar-fraction (3/4)² squares both the top and the bottom. Without parentheses, the exponent attaches only to the nearest number.
print((3 / 4) ** 2) # 0.5625: 9/16
print(3 / 4 ** 2) # 0.1875: 3/16, only the 4 is squaredCommon mistakes
| Mistake | What goes wrong | Fix |
|---|---|---|
| Typing `3 + 5 / 2 + 2` for a fraction | Only the 5 is divided | Write `(3 + 5) / (2 + 2)` |
| Cancelling a term, as in `(6 + 3) / 3 = 6 + 1` | Only factors can be cancelled, not terms | Add first: 9 / 3 = 3 |
| Dividing just the first term of the numerator | Treats the bar as covering only one number | Evaluate the whole numerator, then divide |
| Dropping parentheses around a negative numerator | The sign is applied to the wrong part | Keep the parentheses until each part is a single number |
| Reading `1/2/4` as `1/(2/4)` | Division is left to right | Group explicitly |
The cancellation mistake is the most common. You can only cancel a common factor when it multiplies the whole numerator and the whole denominator. A term that is added cannot be cancelled.
print((6 + 3) / 3) # 3.0: correct
print(6 + 3 / 3) # 7.0: the "cancelled" version is a different expressionCancelling is valid here: (2 · 6) / (2 · 3) = 6/3, because the 2 multiplies everything. It is not valid for (2 + 6) / (2 + 3).
A checking routine
- Rewrite the fraction on one line, with parentheses around the numerator and around the denominator.
- Evaluate the numerator by hand, with the full order of operations.
- Evaluate the denominator by hand.
- Divide, and simplify.
- Run the one-line version in Python and compare. Use
Fractionwhen you want an exact fraction instead of a decimal.
from fractions import Fraction
print(Fraction(5 + 3 * 2 ** 2, 10 - 4 * 2)) # 17/2Fraction takes the numerator and denominator as two separate arguments, which matches how the bar groups them. The arguments are evaluated before the fraction is built, so no extra parentheses are needed.
Exercise
Evaluate (7 − 3²) / (2 − 5 + 1) by hand. Then write the one-line Python version.
Show answer
Numerator: 3² = 9, so 7 − 9 = −2. Denominator: 2 − 5 = −3, then −3 + 1 = −2. Result: −2 / −2 = 1. Python: (7 - 3 ** 2) / (2 - 5 + 1), which prints 1.0.
Exercise
A student types 6 + 9 / 3 to calculate the fraction (6 + 9) over 3. What does it give, what is the correct value, and how do you fix it?
Show answer
It gives 6 + 3 = 9.0. The correct value is 15 / 3 = 5. Add parentheses around the numerator: (6 + 9) / 3.
Exercise
Evaluate (1/2 + 1/3) / (1/4) exactly, using the Fraction class.
Show answer
Fraction(1, 2) + Fraction(1, 3) is 5/6. Dividing by Fraction(1, 4) gives 10/3.