What a Fraction Is
A fraction describes a part of a whole, or more generally one whole number divided by another. This lesson explains the numerator and the denominator, shows fractions on the number line, and introduces Python's Fraction type, which does exact fraction arithmetic for you.
Parts of a whole
Cut a pizza into 8 equal slices and eat 3 of them. You ate of the pizza.
3 ← numerator: how many parts you have
───
8 ← denominator: how many equal parts make one whole- The denominator (bottom) says what size the parts are: eighths, here.
- The numerator (top) counts how many of those parts you have.
- The line between them means "divided by": is the same number as .
The parts must be equal. Three slices of a pizza cut into uneven pieces is not of anything.
| Fraction | Read as | Meaning |
|---|---|---|
| one half | 1 of 2 equal parts | |
| two thirds | 2 of 3 equal parts | |
| five quarters | more than one whole: 1 whole and | |
| four quarters | exactly 1 | |
| zero sevenths | 0 |
Proper and improper fractions
- A proper fraction has a numerator smaller than its denominator (, ). Its value is between 0 and 1.
- An improper fraction has a numerator equal to or larger than its denominator (, ). Its value is 1 or more. You can also write it as a mixed number, such as .
There is nothing wrong with improper fractions. In calculations they are usually easier to work with than mixed numbers.
Note: The denominator can never be 0. "Split a whole into 0 equal parts" has no meaning, just like division by zero.
Fractions on the number line
Every fraction is a point on the number line. To place , split the stretch from 0 to 1 into 4 equal steps and walk 3 of them:
0 1/4 2/4 3/4 1 5/4
|---------|---------|---------|---------|---------|
▲Fractions fill in the gaps between the whole numbers. Between any two fractions there is always another one (their average, for example), so the gaps never run out.
Fractions in Python
Python's / operator gives a float, which is a binary approximation. The fractions module gives an exact rational number instead:
from fractions import Fraction
eaten = Fraction(3, 8)
print("eaten:", eaten)
print("numerator:", eaten.numerator, " denominator:", eaten.denominator)
print("as a float:", float(eaten))
print("3 / 8 with plain division:", 3 / 8)
print(Fraction(5, 4) > 1) # improper: more than one whole
print(Fraction(4, 4)) # exactly one, printed as 1
print(Fraction("2/3")) # can be built from a string tooeaten: 3/8
numerator: 3 denominator: 8
as a float: 0.375
3 / 8 with plain division: 0.375
True
1
2/3
Fraction(0, 5) is fine, but Fraction(1, 0) raises ZeroDivisionError, which is exactly the rule from above.
Fraction of a group
A fraction can describe part of a collection as well as part of one object. If 12 of the 30 players in a match are on the red team, the red team is of the players:
from fractions import Fraction
players = ["red"] * 12 + ["blue"] * 18
red_share = Fraction(players.count("red"), len(players))
print("red share:", red_share) # Python simplifies 12/30 for you
print("blue share:", 1 - red_share)
print("check:", red_share + (1 - red_share))red share: 2/5
blue share: 3/5
check: 1
Python stored as straight away. That is called simplifying, and it is the topic of the next lesson.
In software
- Progress bars and loading screens show "" as a fraction of the bar's width.
- Audio and video describe frame rates as fractions: NTSC video runs at frames per second (about 29.97), and FFmpeg stores it exactly as that fraction to avoid drift over long recordings.
- Music software stores note lengths as fractions of a bar: for a quarter note, for a dotted quarter.
- Layouts: CSS grid's
frunit splits leftover space into fractions (1fr 2frgives and ).
Tip: Whenever you need an exact ratio of two integers, for example a game's tick rate or an aspect ratio,
Fractionkeeps it exact. Afloatonly approximates values like .
Exercises
Exercise 1: Name the fraction
A chocolate bar has 12 equal squares. You eat 5 of them. What fraction did you eat, and what fraction is left?
Answer
You ate ; is left.
Solution
The whole is split into 12 equal squares, so the denominator is 12. You have 5 of them, so you ate . The rest is squares, which is . Check: whole bar.
Exercise 2: Proper or improper?
Sort these into proper and improper fractions: , , , , .
Answer
Proper: , . Improper: , , .
Solution
A fraction is proper when (value below 1), improper when .
Exercise 3: Between 0 and 1
On a number line from 0 to 1 split into fifths, which tick mark is ? Name a fraction that lies between and .
Answer
The third tick after 0. For example lies between them.
Solution
Fifths split 0 to 1 into 5 equal steps; is three steps from 0. The average of and lies halfway between them: . In tenths, and , so sits in the middle.
from fractions import Fraction as F
mid = (F(3, 5) + F(4, 5)) / 2
print(mid, F(3, 5) < mid < F(4, 5))7/10 True
Exercise 4: Team shares in code
A server has 40 players: 14 on red, 16 on blue, the rest spectating. Write Python that prints each group's share as a Fraction and checks that the shares add up to 1.
Answer
red , blue , spectators , total 1.
Solution
Spectators: . Each share is , which Fraction simplifies: , , .
from fractions import Fraction
total = 40
groups = {"red": 14, "blue": 16}
groups["spectators"] = total - sum(groups.values())
shares = {name: Fraction(n, total) for name, n in groups.items()}
for name, share in shares.items():
print(name, share)
print("total", sum(shares.values()))red 7/20
blue 2/5
spectators 1/4
total 1
Test yourself
In the fraction , what does the 9 tell you?
- How many parts you have
- How many equal parts make one whole
- How many wholes there are
- The answer of
Which of these is an improper fraction?
What does print(Fraction(10, 4)) show?
- 10/4
- 2.5
- 5/2
- Fraction(10, 4)