Logarithms — The Simplest Idea with the Scariest Name

Logarithms sound intimidating, but you already understand them. Let's prove it.

Part 1: Counting Zeros

You already know what a logarithm is — you just don't know that you know it yet.

How Many Zeros?

Look at these numbers and count how many zeros each one has:

  • 10
  • 100
  • 1000
  • 10000

How many zeros does 10 have?

How many zeros does 100 have?

How many zeros does 1000 have?

Do you see the pattern?

Powers of 10 in Python

In Python, ** is the power operator. 10**2 means "10 to the power of 2" which is 100.

Try these in Python and observe the pattern:

  • 10**1
  • 10**2
  • 10**3
  • 10**4

What do you notice? Each power adds one more zero!

Predict the Log

Now go the other way. If someone asks "what is log(100000)?", you are really asking: "how many zeros does 100000 have?" or equivalently "10 to the power of WHAT gives 100000?"

Predict the answer, then verify by computing 10**your_answer in Python.

What you just did is called "taking the logarithm base 10." When you said log(1000) = 3, you were saying: "the power of 10 that gives 1000 is 3." Simple, right?

Part 2: The Power Question

Let's reframe logarithms as a question: "10 to the power of WHAT gives this number?"

Flip It Around

Answer each question without Python — just think:

10 to the power of what gives 100?

10 to the power of what gives 1000?

10 to the power of what gives 10?

10 to the power of what gives 1? (Tricky! What happens with power 0?)

What About 500?

Here's where it gets interesting. What is log(500)?

You know log(100) = 2 and log(1000) = 3. So log(500) must be somewhere between 2 and 3. But where exactly?

Use Python's ** operator to do hit-and-trial. Try different values of x and see if 10**x gets close to 500.

Check with Python's Built-in

Python has a built-in function math.log10() that computes the exact log base 10. Let's check how close your hit-and-trial answer was.

More Hit-and-Trial

Now try the same hit-and-trial approach for these:

  1. What is log10(50)? (It's between 1 and 2. Try values of x where 10**x gets close to 50.)
  2. What is log10(5)? (It's between 0 and 1.)

After your guesses, verify both with math.log10().

Key insight: log10(5) ≈ 0.699, log10(50) ≈ 1.699, log10(500) ≈ 2.699. Each time you multiply the number by 10, the log goes up by exactly 1. That makes sense — you're adding one more power of 10!

Part 3: Beyond Base 10 — Any Base Works

So far we've only used 10 as the base. But there's nothing special about 10 — it's just what humans picked because we have 10 fingers. Logarithms work with ANY base.

Log Base 2

Instead of asking "10 to the power of what?", let's ask "2 to the power of what?"

This is called log base 2, written as log2.

log2(4) = ? In other words: 2 to the power of what gives 4?

log2(8) = ? i.e. 2**x = 8, what is x?

log2(16) = ?

log2(2) = ?

Verify with Python

Use Python's ** operator to verify your answers. Compute 2**2, 2**3, 2**4, and 2**1.

Then try: what is log2(32)? log2(64)? log2(1024)?

The Power of Zero

What is log2(1)?

Hint: what is ANY number raised to the power of 0? Try 2**0, 10**0, 5**0 in Python.

Other Bases

Let's try other bases:

  1. What is log3(27)? i.e. 3 to the power of what gives 27?
  2. What is log5(125)? i.e. 5 to the power of what gives 125?
  3. What is log4(64)?

Compute using ** first, then verify with Python's math.log(value, base).

The pattern: log_base(value) always asks the same question — "base to the power of WHAT gives value?" It doesn't matter if the base is 2, 3, 5, 10, or anything else. The idea is always the same.

Part 4: Fractional Logs — Between the Integers

So far all our answers have been nice whole numbers. But what happens when the answer isn't a whole number?

Narrowing It Down

What is log2(6)?

You know that log2(4) = 2 and log2(8) = 3. Since 6 is between 4 and 8, log2(6) must be between 2 and 3.

But where between 2 and 3?

Is log2(6) closer to 2 or to 3? (Is 6 closer to 4 or to 8?)

Try 2**2.5 in your head or on paper. Is it bigger or smaller than 6?

Hit-and-Trial for log2(6)

Use 2**x with different values of x to get as close to 6 as you can. Then check your answer with math.log(6, 2).

Automate the Trial — Write my_log()

You've been doing hit-and-trial by hand. Let's make Python do it for you!

Write a function my_log(value, base) that finds the log by brute force: try x from 0 to 20 in small steps (say 0.001) and return the x where base**x is closest to value.

What you just built is a brute-force logarithm calculator! It's slow (checking 20,000 values), but it works. In the Binary Search chapter, you'll learn how to find this answer in just 40 steps instead of 20,000 — that's the power of binary search.

Summary

Here's everything you discovered in this chapter.

Logarithm = the reverse of exponentiation.

When someone says log_b(x) = y, they're saying: b to the power of y equals x.

That's it. That's the whole idea. Now when you see log2(1024) = 10, you know it just means 2^10 = 1024.

Logs You Computed

Question Meaning Answer
log10(10) 10 to the power of ? = 10 1
log10(100) 10 to the power of ? = 100 2
log10(1000) 10 to the power of ? = 1000 3
log10(500) 10 to the power of ? = 500 ~2.699
log2(4) 2 to the power of ? = 4 2
log2(8) 2 to the power of ? = 8 3
log2(1024) 2 to the power of ? = 1024 10
log3(27) 3 to the power of ? = 27 3
log of 1 (any base) any number to the power of ? = 1 0