Kodeclik Blog
Finding logarithms using Python’s math.log10()
Python’s math.log10() function returns the base-10 logarithm of a number. The syntax for this function is relatively straightforward: it takes one argument which is the number whose logarithm you are trying to compute.
For instance, the log10 of 100 is 2 because 10 squared is 100. Similarly, the log10 of 1000 is 3 because 10 cubed is 1000. Recall that you can only compute logarithms of positive numbers greater than 0; if you try to use it with a negative number, then you will get an error message.
Let us write a simple program to do exponentiation and then logarithms immediately after:
import math as m
for i in range(1,11):
y = 10**i
z = m.log10(y)
print(str(i) +
" -> " +
str(y) +
" -> " +
str(z))
In this program, the loop index goes from 1 to 10 (1 less than the second argument of the range operator, which is 11). Inside the loop, we are first computing 10 raised to the power of the index. Then we are computing the log10 of that answer (which should give us back the original number). The output is:
1 -> 10 -> 1.0
2 -> 100 -> 2.0
3 -> 1000 -> 3.0
4 -> 10000 -> 4.0
5 -> 100000 -> 5.0
6 -> 1000000 -> 6.0
7 -> 10000000 -> 7.0
8 -> 100000000 -> 8.0
9 -> 1000000000 -> 9.0
10 -> 10000000000 -> 10.0
as expected. Note that the logarithms are in floating point notation even though we began with integers.
One important way to understand logarithms is to see how they are distributed. Let us try to find logarithms (to the base 10) of numbers from 1 to 10. It is clear that the logarithm of 1 is zero (because anything to the power of zero is 1). It is also clear that the logarithm of 10 is 1. But what happens in between? How are the logarithms distributed? Here is a program to explore this space:
import math as m
for i in range(1,11):
print(m.log10(i))
The output is:
0.0
0.3010299956639812
0.47712125471966244
0.6020599913279624
0.6989700043360189
0.7781512503836436
0.8450980400142568
0.9030899869919435
0.9542425094393249
1.0
As can be seen the output starts from 0 and grows to 1 but notice the big jump from 0 to 0.3, followed by progressively smaller jumps, till we reach 10.
Let us explore the space of numbers from 10 to 100 similarly:
import math as m
for i in range(10,101,10):
print(m.log10(i))
Note that in the above program we have updated the arguments of the range() operator to start from 10 and go till 100 (one less than 101) but in steps of 10. The output is actually very similar to what we saw earlier:
1.0
1.3010299956639813
1.4771212547196624
1.6020599913279625
1.6989700043360187
1.7781512503836436
1.845098040014257
1.9030899869919435
1.9542425094393248
2.0
We see a big jump from 1 to 1.3 and then smaller jumps till we reach 2.
Thus the moral of the story is that logarithms (to any base) reduce the variation you see in your original quantity of interest and compress it into a smaller range.
In conclusion, the math.log10() function offers a simple yet powerful way to calculate base-10 logarithms within your Python programs.
If you liked learning about math.log10, checkout Python's lcm() function and the log2() function in Python, both from the Math module!
For more Python content, checkout the math.ceil() and math.floor() functions! Also
learn about the math domain error in Python and how to fix it!
Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.