In Python, to calculate the logarithm of a number with base 10, we can use the Python log10() function from the Python math module.
log_base_10 = log10(x)
You can also use the log() function from the Python math module, but this will be slightly less accurate.
log_base_10 = log(x,10)
The Python math module has many powerful functions which make performing certain calculations in Python very easy.
One such calculation which is very easy to perform in Python is finding the logarithm of a number with different bases.
We can find the logarithm of a number with base 10 easily with the Python math module log10() function.
The log10() function takes in a positive number and returns the log of that number base 10.
Below are a few examples of how to use the log10() function to find the logarithm of different numbers with base 10 in Python.
import math
print(math.log10(10))
print(math.log10(50))
print(math.log10(100))
print(math.log10(1500))
print(math.log10(127329))
#Output:
1.0
1.6989700043360187
2.0
3.1760912590556813
5.104927328285411
Using math.log() to Find Logarithms of Numbers in Python
We can also use the Python log() function from the math module to calculate logarithms. By default, the base is the number “e” (2.7172…), but we can change the base of the number by passing a second number to the log() function.
To get the log of a number base 10 with the log() function, we pass ’10’ as the second argument.
Below are some examples of how to calculate the logarithm of different numbers with base 10 in Python using the log() function.
import math
print(math.log(10,10))
print(math.log(50,10))
print(math.log(100,10))
print(math.log(1500,10))
print(math.log(127329,10))
#Output:
1.0
1.6989700043360185
2.0
3.176091259055681
5.10492732828541
As you can see, when comparing with the first example, there are some slight differences when using the log() function. As stated in the Python documentation, this can happen and log10() is generally more accurate than log() for calculating logarithms for numbers with base 10.
Hopefully this article has been beneficial for you to learn how to use the Python log10() function to find the logarithm of a number with base 10.