Lesson 2 of 12 · Numbers and Algebra
Numbers and Operations
Refresh the basics: kinds of numbers, order of operations, negative numbers, fractions, percentages, absolute value, rounding and why computers get 0.1 + 0.2 slightly wrong.
- 8 min read
- Beginner
- Reviewed
- The kinds of numbers: whole numbers, integers and real numbers
- The order of operations, so every calculation is read the same way
- How to work with negative numbers, fractions and percentages
- What absolute value is
- Why computers sometimes give tiny rounding errors
Kinds of numbers
Maths sorts numbers into families. You will see these names in AI papers and documentation.
In Python, whole numbers are int and decimal numbers are float.
The order of operations
Is 2 + 3 × 4 equal to 20 or 14? Everyone must agree, so there is a fixed order.
| Step | Operation | Python |
|---|---|---|
| 1 | Brackets | ( ) |
| 2 | Orders, also called powers | ** |
| 3 | Division and Multiplication, left to right | / * |
| 4 | Addition and Subtraction, left to right | + - |
print(2 + 3 * 4) # multiplication first: 2 + 12 print((2 + 3) * 4) # brackets first: 5 * 4 print(2 * 3 ** 2) # power first: 2 * 9 print(10 - 4 - 3) # left to right: 6 - 3
When in doubt, add brackets. They cost nothing and make your meaning clear.
Negative numbers
The sign rules for multiplying and dividing:
| Calculation | Result | Example |
|---|---|---|
| positive × positive | positive | 3 × 2 = 6 |
| negative × positive | negative | -3 × 2 = -6 |
| negative × negative | positive | -3 × -2 = 6 |
print(-3 * 2) print(-3 * -2) print(5 - 8) print(-2 ** 2) # careful: this is -(2 ** 2) print((-2) ** 2)
The last two lines show a classic trap: the power is done before the minus sign, so write brackets when you mean "minus two, squared".
Absolute value
Absolute value is how you measure how wrong a prediction is, without caring whether it was too high or too low:
actual = 100
for predicted in [90, 110, 100]:
error = predicted - actual
print(f"predicted {predicted}: error {error}, size of error {abs(error)}")Fractions, decimals and percentages
These are three ways of writing the same idea: a part of a whole.
| Fraction | Decimal | Percentage |
|---|---|---|
| 1/2 | 0.5 | 50% |
| 1/4 | 0.25 | 25% |
| 3/4 | 0.75 | 75% |
| 1/10 | 0.1 | 10% |
correct = 172
total = 200
accuracy = correct / total
print("As a decimal:", accuracy)
print("As a percentage:", accuracy * 100, "%")
print(f"Formatted: {accuracy:.1%}") # :.1% shows a percentage with 1 decimal placeold_error = 0.40
new_error = 0.30
change = (new_error - old_error) / old_error * 100
print(f"The error changed by {change:.0f}%")Rounding, and a computer surprise
Run this:
print(0.1 + 0.2) print(0.1 + 0.2 == 0.3) print(round(0.1 + 0.2, 2))
This is not a Python bug: every programming language does it. The fix is to round before showing results, and never to test decimals for exact equality. Python's math.isclose() checks whether two numbers are close enough.
Common mistakes
Turn raw test results into accuracies and a percentage change, the way model reports do.
# A model's test results over three weeks week1_correct, week1_total = 150, 200 week2_correct, week2_total = 168, 200 # 1. Work out each week's accuracy as a percentage. # 2. Work out the percentage change from week 1 to week 2. # Print each value rounded to 1 decimal place, exactly like this: # Output must be exactly: # Week 1 accuracy: 75.0% # Week 2 accuracy: 84.0% # Change: 12.0%
What is 2 + 3 × 4?
Summary
- Natural numbers count, integers include negatives, and real numbers fill the whole number line.
- The order of operations: brackets, powers, multiply and divide, add and subtract.
- Negative times negative is positive. Absolute value measures size without sign.
- Fractions, decimals and percentages are three views of the same thing.
- Computers have tiny floating-point errors, so round results and avoid
==on decimals.
Next: variables and equations, the language for describing unknown values.
Finished reading? Tick it off to track your progress.