The math.log() method is used to calculate the logarithm of a number to the base or calculates the natural logarithm of a number if the user does not specify the base.
Syntax
import math
math.log(x, base)
Parameters
num(required): The number you want to calculate the logarithm for. It must be a positive number.
base(optional): The base of the logarithm. If not specified, the natural logarithm (base e) is calculated
Return Value
Returns a float value.
If the num is negative number or 0, it raises a ValueError. If the num is not a number, it raises a TypeError.
Visual Representation
Example 1: Natural Logarithm (base e)
import math
# Calculate the natural logarithm
print(math.log(5))
print(math.log(3.7))
print(math.log(1))
Output
1.6094379124341003
1.308332819650179
0.0
Example 2: Logarithm with a Specified Base
import math
print(math.log(5,10))
print(math.log(3.7,10))
print(math.log(1,10))
Output
0.6989700043360187
0.568201724066995
0.0