Fractions are the topic most people say "lost them" at school, and it's no wonder. They're often taught as a list of rules to memorise. But fractions are just a way of describing parts of a whole: half a pizza, three quarters of a tank of fuel, 2 out of every 5 customers. Once you see that picture, the rules make sense.
Fractions matter in data science because every proportion, probability and rate is a fraction. "45 of 300 customers churned" is the fraction 45/300. A probability of ¼ is a fraction. Understanding fractions makes percentages, probability and statistics much easier. This lesson explains fractions from scratch, then shows how to compare, simplify, add, subtract, multiply and divide them, step by step, with Python to check every answer.
What you need first: multiplication and division, and negative numbers for a couple of examples.
Key terms
- Fraction: a number written as one number over another, like ¾, meaning 3 divided by 4.
- Numerator: the top number: how many parts you have.
- Denominator: the bottom number: how many equal parts the whole is split into.
- Equivalent fractions: different-looking fractions with the same value, like ½ and 2/4.
- Simplest form: a fraction that can't be simplified further, like ¾ rather than 6/8.
- Reciprocal: a fraction flipped upside down. The reciprocal of ¾ is 4/3.
What a fraction means
Cut a pizza into 4 equal slices and eat 3. You've eaten ¾ (three quarters):
- The denominator 4 says the pizza was cut into 4 equal slices.
- The numerator 3 says how many slices you took.
The fraction bar also means divide: ¾ = 3 ÷ 4 = 0.75. So every fraction is also a decimal, and every fraction describes a proportion: "3 out of 4".
Some special cases:
- If the top and bottom are equal, you have the whole thing: 4/4 = 1.
- If the top is bigger, you have more than one whole: 5/4 = 1¼ (one whole pizza and a quarter). These are called improper fractions and mixed numbers.
- 0/4 = 0: you took nothing.
- 4/0 is undefined: you can't split something into zero parts. Python gives an error.
Equivalent fractions
Cut the same pizza into 8 slices instead of 4, and eat 6 of them. You've eaten the same amount: 6/8 = ¾.
Multiply or divide the top and bottom by the same number, and the value doesn't change:
- ¾ = (3 × 2)/(4 × 2) = 6/8
- ¾ = (3 × 25)/(4 × 25) = 75/100, which is 75%
The second one shows why fractions and percentages are the same idea: a percentage is a fraction out of 100. Percentages explained builds on this.
Simplifying fractions
To simplify, divide the top and bottom by a number that goes into both. Repeat until no number (except 1) divides both.
Simplify 18/24:
- Both are even: divide by 2, giving 9/12.
- Both divide by 3: 3/4.
- Nothing else divides 3 and 4, so ¾ is the simplest form.
The fastest way is to divide by the greatest common divisor (GCD), the biggest number that divides both. For 18 and 24, it's 6: 18 ÷ 6 = 3 and 24 ÷ 6 = 4.
import math
from fractions import Fraction
print(math.gcd(18, 24))
print(Fraction(18, 24)) # Fraction simplifies automatically
print(Fraction(45, 300))
6
3/4
3/20
"45 of 300 customers churned" simplifies to 3/20: 3 in every 20 customers.
Comparing fractions
Which is bigger, ⅗ or ⅔? The bottoms differ, so the slices are different sizes and you can't compare directly. Two easy methods:
Method 1: convert to decimals. ⅗ = 0.6 and ⅔ ≈ 0.667, so ⅔ is bigger.
Method 2: use a common denominator. Rewrite both as fractions of 15: ⅗ = 9/15 and ⅔ = 10/15. Now the slices are the same size, and 10 slices beat 9.
print(3 / 5, 2 / 3)
print(Fraction(3, 5) < Fraction(2, 3))
0.6 0.6666666666666666
True
Adding and subtracting fractions
Same denominator: add the tops
If the slices are the same size, just count them: ⅕ + ⅖ = ⅗. The bottom stays the same, because the slice size doesn't change.
Never add the bottoms: ⅕ + ⅖ is not 3/10.
Different denominators: make them the same first
½ + ⅓: halves and thirds are different-sized slices, so first cut both into slices of the same size.
- Find a number both denominators divide into: 6 works (2 × 3).
- Rewrite: ½ = 3/6 and ⅓ = 2/6.
- Add the tops: 3/6 + 2/6 = 5/6.
¾ − ⅙:
- A common denominator of 4 and 6 is 12.
- ¾ = 9/12 and ⅙ = 2/12.
- Subtract: 9/12 − 2/12 = 7/12.
A quick shortcut that always works: multiply the two denominators to get a common one. It may give bigger numbers, so simplify at the end.
print(Fraction(1, 2) + Fraction(1, 3))
print(Fraction(3, 4) - Fraction(1, 6))
5/6
7/12
Multiplying fractions
Multiplying is the easiest operation: multiply the tops, multiply the bottoms.
⅔ × ¾ = (2 × 3)/(3 × 4) = 6/12 = ½
What does it mean? "⅔ of ¾". If ¾ of a class passed, and ⅔ of those passed with distinction, then ½ of the class got a distinction. In maths, "of" means multiply.
To multiply a fraction by a whole number, write the whole number over 1: 3 × ⅖ = 3/1 × ⅖ = 6/5.
This idea is the heart of probability. If ½ of emails contain a link and ⅕ of those are spam, then ½ × ⅕ = 1/10 of all emails are spam emails with links. You'll see this again in conditional probability.
Dividing fractions
To divide by a fraction, multiply by its reciprocal (flip the second fraction):
¾ ÷ ½ = ¾ × 2/1 = 6/4 = 3/2 = 1½
Why does this work? Dividing asks "how many of this fit into that?". How many halves fit into ¾ of a pizza? One half fits, with a quarter left over, and that quarter is half of another half. So 1½ halves fit.
A simpler example: 3 ÷ ½ asks "how many halves are in 3?". Six, because each whole contains two halves. And 3 × 2/1 = 6. Dividing by ½ is the same as multiplying by 2.
print(Fraction(3, 4) / Fraction(1, 2))
print(3 / Fraction(1, 2))
3/2
6
Fractions, decimals and percentages
The same value can be written three ways:
| Fraction | Decimal | Percentage |
|---|---|---|
| ½ | 0.5 | 50% |
| ¼ | 0.25 | 25% |
| ¾ | 0.75 | 75% |
| ⅕ | 0.2 | 20% |
| 1/10 | 0.1 | 10% |
| ⅓ | 0.333… | 33.3% |
| 1/100 | 0.01 | 1% |
To convert: fraction → decimal, divide the top by the bottom. Decimal → percentage, multiply by 100.
Fractions in data science
- Proportions: the share of customers who churned, the share of emails that are spam:
(df["churn"] == "Yes").mean()is the fraction of rows where churn is "Yes". - Probabilities: always between 0 and 1, and usually computed as fractions: favourable outcomes ÷ all outcomes.
- Train/test splits:
test_size=0.2means ⅕ of the data is held back for testing; see train, validation and test sets. - Averages: the mean is a fraction: the total divided by the count.
Practice
- Simplify 24/36.
Show the answer
The GCD of 24 and 36 is 12, so 24/36 = 2/3.
- Which is bigger: ⅝ or ⅗?
Show the answer
⅝ = 0.625 and ⅗ = 0.6, so ⅝ is bigger. Or with a common denominator of 40: 25/40 against 24/40.
- Calculate: (a) ⅖ + 1/10 (b) ⅚ − ¼ (c) ⅗ × 5/6 (d) ⅔ ÷ 4
Show the answer
(a) 4/10 + 1/10 = 5/10 = ½. (b) 10/12 − 3/12 = 7/12. (c) 15/30 = ½. (d) ⅔ × ¼ = 2/12 = ⅙.
- In a dataset of 1,200 rows, 180 have a missing value. What fraction is missing, in simplest form, and what percentage is that?
Show the answer
180/1200 simplifies (divide by 60) to 3/20, which is 0.15, or 15%.
Key takeaways
- A fraction describes parts of a whole: numerator (parts taken) over denominator (equal parts in the whole).
- Multiplying or dividing top and bottom by the same number gives an equivalent fraction.
- Add and subtract only with the same denominator; multiply straight across; divide by multiplying by the reciprocal.
- "Of" means multiply: ⅔ of ¾ is ⅔ × ¾.
- Proportions, probabilities and averages in data science are all fractions.
Frequently asked questions
Why do you need a common denominator to add fractions?
The denominator sets the size of each slice. You can only count slices together when they're the same size, so you first rewrite both fractions with the same denominator.
Why do you flip the fraction when dividing?
Dividing by a number is the same as multiplying by its reciprocal. Dividing by ½ asks how many halves fit, which is the same as doubling, or multiplying by 2/1.
How do I convert a fraction to a percentage?
Divide the top by the bottom to get a decimal, then multiply by 100. For example, 3/8 = 0.375 = 37.5%.
What to learn next
- Learn to work with decimals and round them sensibly in decimals, rounding and significant figures.
- Revisit negative numbers explained.