How many times do you have to double 1 to reach 1,000? How many digits does a 13-digit number have? (Trick question, but the method matters.) How many times can you halve a list of a million names before you're down to one? How many years until your savings double?
All four are the same kind of question: what power do I need? Exponents answer "what do I get if I multiply 2 by itself 10 times?" (1,024). Logarithms answer the reverse: "how many times must I multiply 2 by itself to get 1,024?" (10).
Logarithms have a reputation for being hard, but they're just that one question. This lesson builds the idea step by step, covers the three logarithms you'll actually use (base 10, base 2 and the natural log, ln), shows how to solve for an unknown exponent, and explains why logs are everywhere in data science.
What you need first: exponents and powers and inverse functions.
Key terms
- Logarithm: the power you raise a base to, to get a number.
- Base: the number being raised to a power.
- log₁₀: the common logarithm, base 10.
- log₂: the binary logarithm, base 2.
- ln: the natural logarithm, base e ≈ 2.718.
The one question
log_b(x) asks: "b to what power gives x?"
- log₂(8) = 3, because 2³ = 8.
- log₁₀(1000) = 3, because 10³ = 1,000.
- log₅(25) = 2, because 5² = 25.
- log₂(1024) = 10, because 2¹⁰ = 1,024.
The small number (the base) and the answer (the exponent) are the same two things you see in a power, just rearranged:
| Exponent form | Logarithm form |
|---|---|
| 2³ = 8 | log₂(8) = 3 |
| 10² = 100 | log₁₀(100) = 2 |
| 3⁴ = 81 | log₃(81) = 4 |
| bʸ = x | log_b(x) = y |
A logarithm is the inverse of an exponential: it undoes it, as in inverse functions. Exponentials take you from the power to the number; logs take you back.
Logs of special numbers
| Number | log₁₀ | Why |
|---|---|---|
| 1,000 | 3 | 10³ |
| 10 | 1 | 10¹: the log of the base is always 1 |
| 1 | 0 | 10⁰: the log of 1 is always 0 |
| 0.1 | −1 | 10⁻¹: numbers below 1 have negative logs |
| 0.001 | −3 | 10⁻³ |
| 0 or negative | undefined | no power of 10 is zero or negative |
The last row matters in practice: you can't take the log of zero or a negative number. That's why data scientists use log(1 + x) for data with zeros, as you'll see in log scales.
Logs between the whole numbers
log₁₀(500) isn't a whole number. 10² = 100 and 10³ = 1,000, so it must be between 2 and 3 (it's about 2.7). Most logarithms are decimals like this, and a computer works them out.
The three logs you'll use
| Log | Base | Where it's used | Python |
|---|---|---|---|
| log₁₀ | 10 | counting digits, orders of magnitude, decibels, earthquake scales | math.log10 |
| log₂ | 2 | computer science: bits, halving, binary search, decision tree entropy | math.log2 |
| ln | e ≈ 2.718 | statistics and ML: growth, log loss, likelihood | math.log |
import math
print(math.log10(1000), math.log2(8), math.log(math.e))
print(math.log10(500))
print(math.log(100, 10))
try:
math.log(0)
except ValueError as error:
print("Error:", error)
3.0 3.0 1.0
2.6989700043360187
2.0
Error: math domain error
math.log(x, base) handles any base. Careful: in Python, NumPy and most ML papers, plain log means ln, the natural log. On calculators and in school, "log" often means log₁₀.
What logs measure: "how many times"
Counting digits
A number's log₁₀ tells you its size in orders of magnitude. Numbers from 100 to 999 have logs from 2 to just under 3, and have 3 digits. In general:
digits = ⌊log₁₀(x)⌋ + 1 (for whole numbers x ≥ 1)
for x in [7, 450, 9_999, 3_200_000]:
print(x, math.floor(math.log10(x)) + 1)
7 1
450 3
9999 4
3200000 7
Counting halvings: binary search
How many times can you halve 1,000,000 before you reach 1? That's log₂(1,000,000):
print(math.log2(1_000_000))
19.931568569324174
About 20. That's why binary search finds any name in a sorted list of a million in about 20 steps: each step halves the list. It's an O(log n) algorithm, and logs are why it's so fast; see Big O notation.
Solving for an exponent
This is where logs earn their keep. ₹1,000 grows at 8% a year. When does it reach ₹2,000?
The equation is 1.08ᵗ = 2. The unknown is in the exponent, so normal equation solving can't get at it. Logs can:
t = log(2) / log(1.08)
(You'll see why this works in logarithm rules. It works with any base, as long as you use the same one on top and bottom.)
print(math.log(2) / math.log(1.08))
print(math.log(1000) / math.log(2))
9.006468342000588
9.965784284662087
- Doubling at 8% takes 9.006 years: the exact answer behind the "rule of 72" estimate of 9 from exponential growth.
- 2ᵗ = 1,000 when t ≈ 9.97: you need just under 10 doublings to reach 1,000, as 2¹⁰ = 1,024 confirms.
The log curve
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0.05, 16, 400)
fig, ax = plt.subplots()
ax.plot(x, np.log2(x), lw=2, label="y = log₂(x)")
for point in [1, 2, 4, 8, 16]:
ax.plot(point, np.log2(point), "o", color="black")
ax.annotate(f"({point}, {int(np.log2(point))})", (point, np.log2(point)),
xytext=(5, -15), textcoords="offset points")
ax.axhline(0, color="grey", lw=0.8)
ax.axvline(0, color="grey", lw=0.8)
ax.set_title("Each doubling of x adds just 1 to log₂(x)")
ax.legend()
plt.show()
The key feature: multiplying x by 2 only adds 1 to the log. Going from 8 to 16 adds the same as going from 1 to 2. Logs turn multiplication into addition, and that one property is why they're so useful.
Why data scientists love logs
- Taming huge ranges: incomes from ₹10,000 to ₹10 crore become logs from 4 to 8. See why data scientists use log scales.
- Turning products into sums: probabilities get multiplied, which creates impossibly tiny numbers; their logs just get added. See logarithm rules.
- Log loss: the standard loss for classifiers is −ln(p), the log of the probability given to the right answer. See classification metrics.
- Entropy: decision trees measure how mixed a group is with log₂.
Practice
- Work out: (a) log₂(16) (b) log₁₀(10,000) (c) log₃(27) (d) log₁₀(0.01)
Show the answer
(a) 4 (b) 4 (c) 3 (d) −2
- Write in log form: 5³ = 125. Write in exponent form: log₂(64) = 6.
Show the answer
log₅(125) = 3. 2⁶ = 64.
- Between which two whole numbers is log₁₀(4,500)?
Show the answer
Between 3 and 4, because 10³ = 1,000 and 10⁴ = 10,000. (It's about 3.65.)
- How many steps does binary search need, at most, for a sorted list of 1,000 items?
Show the answer
log₂(1,000) ≈ 9.97, so about 10 steps.
- A city's population grows 3% a year. Write an expression for the years needed to double, and evaluate it.
Show the answer
t = log(2) / log(1.03) ≈ 23.4 years. (The rule of 72 estimates 72 ÷ 3 = 24.)
Key takeaways
- log_b(x) answers "b to what power gives x?"; it's the inverse of bˣ.
- log(1) = 0, log of the base = 1, numbers below 1 have negative logs, and log(0) is undefined.
- log₁₀ measures orders of magnitude, log₂ counts doublings and halvings, ln uses base e.
- In Python,
math.logandnp.logare natural logs. - To solve bᵗ = k, use t = log(k)/log(b).
- Logs turn multiplication into addition.
Frequently asked questions
What is a logarithm in simple terms?
The answer to "what power do I raise this base to, to get this number?". log₂(8) = 3, because 2 raised to the power 3 is 8.
Why can't you take the log of zero?
Because no power of a positive base equals zero. Powers get closer and closer to zero as the exponent becomes more negative, but never reach it, so log(0) is undefined.
Which logarithm does Python's math.log use?
The natural logarithm, base e. Use math.log10 for base 10, math.log2 for base 2, or math.log(x, base) for any other base.
What to learn next
- Learn the rules for working with logs in logarithm rules, with examples.
- Revisit the number e explained.