You already know what a logarithm is — you just don't know that you know it yet.
Look at these numbers and count how many zeros each one has:
How many zeros does 10 have?
How many zeros does 100 have?
How many zeros does 1000 have?
Do you see the pattern?
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**110**210**310**4What do you notice? Each power adds one more zero!
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?
Let's reframe logarithms as a question: "10 to the power of WHAT gives this number?"
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?)
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.
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.
Now try the same hit-and-trial approach for these:
10**x gets close to 50.)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!
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.
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) = ?
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)?
What is log2(1)?
Hint: what is ANY number raised to the power of 0? Try 2**0, 10**0, 5**0 in Python.
Let's try other bases:
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.
So far all our answers have been nice whole numbers. But what happens when the answer isn't a whole number?
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?
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).
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.
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.
| 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 |
You just invented logarithms from scratch. Every time you see "log", just ask yourself: "what power of the base gives this number?" That's all there is to it.
Source on GitHub · Back to all chapters
© 2026 CloudxLab. All rights reserved.