The Number Line and Ordering
The number line is a straight line on which every real number has its own point: zero in the middle, positive numbers to the right, negative numbers to the left. It turns "which number is bigger?" into "which point is further right?", and it is the picture behind ordering, sorting, distances and, later, graphs.
Drawing the line
Pick a point and call it 0. Pick a unit length and mark 1, 2, 3, … to the right at equal steps, and −1, −2, −3, … to the left. Fractions and decimals sit between the marks: halfway between 0 and 1, −2.5 halfway between −3 and −2.
◀──┼────┼────┼────┼────┼────┼────┼────┼────┼──▶
−4 −3 −2 −1 0 1 2 3 4
▲ ▲ ▲
−2.5 ½ 1.5The arrows at both ends say the line goes on forever. There is no biggest and no smallest number.
Comparing numbers
On the number line, a number is greater than every number to its left. We write comparisons with these symbols:
| Symbol | Meaning | True example |
|---|---|---|
| less than | ||
| greater than | ||
| less than or equal to | ||
| greater than or equal to | ||
| equal to | ||
| not equal to |
A memory trick: the open, wide side of < faces the bigger number. With negatives, intuition can slip: −10 is less than −2, because −10 lies further left. Think of temperatures: −10 °C is colder than −2 °C.
Python has the same comparisons, written <, >, <=, >=, == and !=. Each one answers True or False:
print(-10 < -2)
print(0.5 == 1/2)
print(3 >= 3, 3 > 3)
print(-1 != 1)
print(-4 < -1 < 2) # chained: both comparisons must holdTrue
True
True False
True
True
The last line is a chained comparison: Python reads -4 < -1 < 2 as " and ", just as mathematicians do.
Ordering a list
Putting numbers in order from smallest to largest (ascending) or largest to smallest (descending) is reading the number line from left to right or right to left. Python's sorted does it for you:
temps = [3, -7, 0, 12, -1.5, 5, -7]
print("ascending: ", sorted(temps))
print("descending:", sorted(temps, reverse=True))
print("smallest:", min(temps), " largest:", max(temps))ascending: [-7, -7, -1.5, 0, 3, 5, 12]
descending: [12, 5, 3, 0, -1.5, -7, -7]
smallest: -7 largest: 12
Distance between two points
The distance between two numbers on the line is the larger minus the smaller, which is always positive or zero. From −3 to 4 the distance is steps. In Python, abs(a - b) gives the distance whichever order you use; the Absolute Value lesson explains why.
Intervals describe a stretch of the line. "All numbers with " includes 1 but not 4. Programmers meet this exact shape all the time: range(1, 4) gives 1, 2, 3, including the start and stopping before the end.
Seeing the line
A picture helps. This plot marks a few values on a number line:
import matplotlib.pyplot as plt
values = [-3, -1.5, 0, 0.5, 2, 3.25]
plt.figure(figsize=(7, 2))
plt.axhline(0, color="gray", linewidth=1)
plt.scatter(values, [0] * len(values), color="tab:blue", zorder=3)
for v in values:
plt.annotate(str(v), (v, 0), textcoords="offset points", xytext=(0, 8), ha="center")
plt.xticks(range(-4, 5))
plt.yticks([])
plt.xlim(-4.5, 4.5)
plt.title("Points on a number line")
plt.show()
print("plotted", len(values), "points from", min(values), "to", max(values))plotted 6 points from -3 to 3.25
In software
- Sorting is one of the most common tasks in programming, and every sort compares two values at a time with
<. The Data Structures & Algorithms course shows how sorting algorithms do it. - Clamping keeps a value inside an interval: a health bar between 0 and 100, a slider between its minimum and maximum. In Python:
max(lo, min(x, hi)). - Timelines and scroll positions are number lines: an audio player's seek bar maps seconds onto pixels along a line.
def clamp(x, lo, hi):
return max(lo, min(x, hi))
for health in [-20, 35, 100, 140]:
print(health, "->", clamp(health, 0, 100))-20 -> 0
35 -> 35
100 -> 100
140 -> 100
Exercises
Exercise 1: Fill in the symbol
Put , or between each pair: (a) (b) (c) (d)
Answer
(a) (b) (c) (d)
Solution
Exercise 2: Order them
Write in ascending order: 1.2, −0.8, −2, , 0, .
Answer
−2, −0.8, , 0, , 1.2
Solution
Convert to decimals: and . On the line from left to right: .
from fractions import Fraction
nums = [1.2, -0.8, -2, Fraction(3, 4), 0, Fraction(-1, 3)]
print([str(x) for x in sorted(nums)])['-2', '-0.8', '-1/3', '0', '3/4', '1.2']
Exercise 3: Distances
Find the distance between (a) −5 and 2, (b) −7 and −3, (c) 4.5 and −1.5.
Answer
(a) 7 (b) 4 (c) 6
Solution
Larger minus smaller:
Exercise 4: Which integers?
List every integer with , then write the Python range that produces exactly those numbers.
Answer
−1, 0, 1, 2, 3, produced by range(-1, 4).
Solution
−2 itself is excluded (strict ), 3 is included (). range includes its start and stops before its end, so the start is −1 and the end is 4.
print(list(range(-1, 4)))[-1, 0, 1, 2, 3]
Test yourself
Which statement is true?
- You cannot compare negative numbers
How many integers satisfy ?
- 4
- 5
- 6
- 3
What does sorted([4, -2, 0, -9]) return?
- [4, 0, -2, -9]
- [-2, -9, 0, 4]
- [-9, -2, 0, 4]
- [0, -2, 4, -9]