TExcelTAD
Home › Basic Math & Pre-Algebra › The Number Line and Ordering

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: 12\frac{1}{2} halfway between 0 and 1, −2.5 halfway between −3 and −2.

text
 ◀──┼────┼────┼────┼────┼────┼────┼────┼────┼──▶
   −4   −3   −2   −1    0    1    2    3    4
             ▲              ▲    ▲
           −2.5             ½   1.5

The 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 −3<1-3 < 1
>> greater than 0>−0.50 > -0.5
≤\le less than or equal to 4≤44 \le 4
≥\ge greater than or equal to 7≥27 \ge 2
== equal to 12=0.5\frac{1}{2} = 0.5
≠\ne not equal to 2≠−22 \ne -2

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:

python · try it yourself
print(-10 < -2)
print(0.5 == 1/2)
print(3 >= 3, 3 > 3)
print(-1 != 1)
print(-4 < -1 < 2)      # chained: both comparisons must hold
True
True
True False
True
True

The last line is a chained comparison: Python reads -4 < -1 < 2 as "−4<−1-4 < -1 and −1<2-1 < 2", 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:

python · try it yourself
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 4−(−3)=74 - (-3) = 7 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 xx with 1≤x<41 \le x < 4" 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:

python · try it yourself
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.
python · try it yourself
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) −6  □  −9-6 \;\square\; -9 (b) 0.4  □  250.4 \;\square\; \frac{2}{5} (c) −1  □  0-1 \;\square\; 0 (d) 2.05  □  2.52.05 \;\square\; 2.5

Answer

(a) >> (b) == (c) << (d) <<

Solution
(a) −6 is to the right of −9→ −6>−9(b) 25=410=0.4→ equal(c) negatives lie left of 0→ −1<0(d) compare tenths: 2.05 has 0, 2.5 has 5→ 2.05<2.5\begin{aligned} &\text{(a) } {-6} \text{ is to the right of } {-9} && \to\ -6 > -9 \\ &\text{(b) } \tfrac{2}{5} = \tfrac{4}{10} = 0.4 && \to\ \text{equal} \\ &\text{(c) negatives lie left of } 0 && \to\ -1 < 0 \\ &\text{(d) compare tenths: } 2.05 \text{ has } 0,\ 2.5 \text{ has } 5 && \to\ 2.05 < 2.5 \end{aligned}

Exercise 2: Order them

Write in ascending order: 1.2, −0.8, −2, 34\frac{3}{4}, 0, −13-\frac{1}{3}.

Answer

−2, −0.8, −13-\frac{1}{3}, 0, 34\frac{3}{4}, 1.2

Solution

Convert to decimals: −13≈−0.333-\frac{1}{3} \approx -0.333 and 34=0.75\frac{3}{4} = 0.75. On the line from left to right: −2<−0.8<−0.333<0<0.75<1.2-2 < -0.8 < -0.333 < 0 < 0.75 < 1.2.

python · try it yourself
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:

(a)2−(−5)=2+5=7(b)−3−(−7)=−3+7=4(c)4.5−(−1.5)=4.5+1.5=6\begin{aligned} &\text{(a)}\quad 2 - (-5) = 2 + 5 = 7 \\ &\text{(b)}\quad -3 - (-7) = -3 + 7 = 4 \\ &\text{(c)}\quad 4.5 - (-1.5) = 4.5 + 1.5 = 6 \end{aligned}

Exercise 4: Which integers?

List every integer xx with −2<x≤3-2 < x \le 3, 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 (≤\le). range includes its start and stops before its end, so the start is −1 and the end is 4.

python · try it yourself
print(list(range(-1, 4)))
[-1, 0, 1, 2, 3]

Test yourself

Which statement is true?

  1. −8>−3-8 > -3
  2. −8<−3-8 < -3
  3. −8=−3-8 = -3
  4. You cannot compare negative numbers

How many integers xx satisfy −3≤x<2-3 \le x < 2?

  1. 4
  2. 5
  3. 6
  4. 3

What does sorted([4, -2, 0, -9]) return?

  1. [4, 0, -2, -9]
  2. [-2, -9, 0, 4]
  3. [-9, -2, 0, 4]
  4. [0, -2, 4, -9]