How to calculate logarithm in Python
To calculate the logarithm of a number with the python language we use the log () function of the math library.
math.log(x,base)
- The first argument in input (x) is the real number of the logarithm.
- The second argument (b) is optional and it indicates the basis of the logarithm.
The function returns in output (y) the logarithm on base b of the number x.
If the base is not specified, the log () function calculates the natural logarithm of the number x.
Note. The log () function is contained in the math module. So, to use it in a script, you must first import it using the import or from import statement.
Examples of calculation
Example 1
In this script the log() function calculates the natural logarithm of 10.
from math import log
log(10)
The output result is
2.302585092994046
So the exponential e2.302585092994046 is equal to 10.
Example 2
This script calculates the logarithm of 100 on a 10 basis.
import math
log(100,10)
The output result is
2.0
The power of the number 102 is equal to 100. So the log of one hundred on a ten basis is two.