Index Ai Academy
Phase 0 · ToolsNumbers for code

Negative numbers and distance from zero

About 20 minLooking ahead: some prerequisites aren't done yet

Why this matters

Below-zero numbers are everywhere in data. A refund is a negative sale, a winter temperature can be −9, a profit can turn into a loss, and a model's scores and weights are negative as often as positive. Python handles negatives exactly, but two of its operators surprise people with them, and those surprises cause real bugs. This lesson builds the number line in your head and shows where the traps are.

  • Place numbers below zero on a number line and compare them
  • Apply the sign rules for adding, subtracting, multiplying and dividing
  • Measure how far off a number is with abs()
  • Predict what // and % do with negative numbers, and use % to wrap around a clock
Quick check
Before any explaining, a guess. // divides and keeps a whole number. What does this print?
print(-7 // 2)

The number line

Picture a ruler that carries on to the left of zero. Numbers to the right are bigger; numbers to the left are smaller. So −1 is bigger than −3, because it sits further right, closer to zero. A temperature of −1 is warmer than −3, and a bank balance of −$1 is better than −$3.

Adding moves you right and subtracting moves you left, so subtracting more than you have takes you below zero:

print(-3 < -1)       # True: -3 is further left
print(3 - 10)        # 7 steps left of zero
print(-5 + 8)        # start at -5, move 8 right

A minus sign in front of a number, like -5, just says which side of zero it's on. It's easy to confuse with subtraction, because it's the same symbol: -5 is a number, while 3 - 5 is a calculation that happens to give a negative number. Zero itself is neither positive nor negative; it's the point where the two sides meet.

Negatives show up wherever something can go either way: profit and loss, a balance that's overdrawn, a change that's a fall rather than a rise, a position below sea level, a score below average.