Kinds of Numbers
Not all numbers are the same kind. Counting numbers, whole numbers, integers, rational numbers, irrational numbers and real numbers are families that fit inside each other like nested boxes. Knowing which family a number belongs to tells you what you can safely do with it, and in code it decides which data type you should use.
The number families
| Family | Symbol | What it contains | Examples |
|---|---|---|---|
| Natural (counting) numbers | 1, 2, 3, 4, … | 1, 7, 365 | |
| Whole numbers | 0 and the natural numbers | 0, 12, 1000 | |
| Integers | whole numbers and their negatives | −40, −1, 0, 5 | |
| Rational numbers | anything that can be written as a fraction of integers () | , , 0.25, 6 | |
| Irrational numbers | real numbers that are not a fraction | , , | |
| Real numbers | all rational and irrational numbers: every point on the number line | everything above |
Note: Some books start the natural numbers at 0. Programmers usually do too: arrays are indexed from 0. When it matters, say "positive integers" (1, 2, 3, …) or "non-negative integers" (0, 1, 2, …) to be clear.
Each family contains the one before it:
The symbol means "is contained in". So 5 is a natural number, but it is also a whole number, an integer, a rational number () and a real number. A number is either rational or irrational, never both.
Why we needed each new family
Each family was invented because the previous one could not answer some question:
- Subtraction needs negatives: has no answer among the whole numbers, so we add the integers (−2).
- Division needs fractions: has no integer answer, so we add the rationals ().
- Square roots need irrationals: no fraction squared gives exactly 2, so must be a new kind of number.
Rational numbers as decimals
Every rational number, written as a decimal, either stops (terminates) or repeats a block of digits forever:
An irrational number's decimal never stops and never settles into a repeating block. goes on without a pattern. That is why a computer can never store exactly: it keeps only the first 16 or so digits.
from fractions import Fraction
import math
print(Fraction(1, 4), "=", 1 / 4)
print(Fraction(5, 7), "=", 5 / 7)
print("0.75 as a fraction:", Fraction("0.75"))
print("pi (as stored):", math.pi)
print("sqrt(2):", math.sqrt(2))
print("sqrt(2) squared:", math.sqrt(2) ** 2)1/4 = 0.25
5/7 = 0.7142857142857143
0.75 as a fraction: 3/4
pi (as stored): 3.141592653589793
sqrt(2): 1.4142135623730951
sqrt(2) squared: 2.0000000000000004
The last line is a giveaway: the computer's is only a rational approximation, so squaring it does not give exactly 2.
Classifying numbers in Python
Python's types line up with the families. Integers are int. Decimal numbers are float, an approximation of a real number. Exact rationals are Fraction. Let's sort a few values:
def classify(x):
kinds = []
if x == int(x):
if x >= 1:
kinds.append("natural")
if x >= 0:
kinds.append("whole")
kinds.append("integer")
kinds.append("rational")
kinds.append("real")
return kinds
for value in [7, 0, -3, 2.5, -0.75]:
print(f"{value:>6}:", ", ".join(classify(value))) 7: natural, whole, integer, rational, real
0: whole, integer, rational, real
-3: integer, rational, real
2.5: rational, real
-0.75: rational, real
The function only works for numbers given as finite decimals, which are always rational. It cannot recognise as irrational: a float has already chopped down to a fraction. Deciding irrationality needs mathematics, not just computation.
Even and odd, positive and negative
Two more ways to sort integers come up constantly:
- Even numbers divide by 2 with no remainder (…, −4, −2, 0, 2, 4, …); odd numbers leave remainder 1 (1, 3, 5, …). In Python:
n % 2 == 0means even. - Positive numbers are greater than 0, negative numbers are less than 0, and 0 is neither.
In software
Choosing a number type is choosing a family:
- Counts and indexes (items in a cart, array positions) are whole numbers: use integers.
- Temperatures, bank balances, coordinates can be negative: use signed integers or floats.
- Exact ratios (recipe scaling, music rhythms, probability by hand) suit fractions.
- Measurements and physics (positions in a game, weights in a neural network) are real numbers, approximated by floats. Every float is secretly a rational number with a power-of-two denominator, which is why Floating Point has surprises.
Exercises
Exercise 1: Sort them
For each number, give the smallest family it belongs to: 12, −8, 0, , , , 0.121212… (repeating).
Answer
12 natural · −8 integer · 0 whole · rational · natural · irrational · 0.1212… rational
Solution
12 counting number → natural
−8 negative, but whole → integer
0 not a counting number → whole
3/5 fraction of integers → rational
√9 = 3 a counting number in disguise → natural
√5 5 is not a perfect square → irrational (2.2360679…)
0.1212… repeating decimal = 12/99 = 4/33 → rationalExercise 2: True or false
(a) Every integer is a rational number. (b) Every rational number is an integer. (c) 0 is a natural number in the convention .
Answer
(a) true, (b) false, (c) false.
Solution
(a) Any integer can be written as , so it is rational. (b) is rational but not an integer. (c) With starting at 1, 0 is a whole number but not a natural number.
Exercise 3: Repeating decimal to fraction
Use Fraction to find the fraction for 0.375 and check that really is 0.121212….
Answer
, and (the block 12 repeats).
Solution
; dividing top and bottom by 125 gives .
from fractions import Fraction
print(Fraction("0.375"))
print(4 / 33)3/8
0.12121212121212122
The last digit 2 is the float's rounding; the true decimal repeats 12 forever.
Exercise 4: Even, odd, sign
Write a loop that prints, for each of −4, −1, 0, 3, 10, whether it is even or odd and whether it is positive, negative or zero.
Answer
−4 even negative · −1 odd negative · 0 even zero · 3 odd positive · 10 even positive
Solution
for n in [-4, -1, 0, 3, 10]:
parity = "even" if n % 2 == 0 else "odd"
if n > 0:
sign = "positive"
elif n < 0:
sign = "negative"
else:
sign = "zero"
print(n, parity, sign)-4 even negative
-1 odd negative
0 even zero
3 odd positive
10 even positive
Python's % always returns 0 or 1 for n % 2, even for negative , so the test works for negatives too.
Test yourself
Which of these numbers is irrational?
- 0.5
Which family is the smallest one that contains −5?
- Natural numbers
- Whole numbers
- Integers
- Irrational numbers
The decimal 0.272727… (27 repeating forever) is…
- irrational, because it never ends
- rational, because it repeats
- not a real number
- an integer