What is math.log() in Python?
The log() function in Python calculates the logarithm of a number to the base or calculates the natural logarithm of a number if the base is not specified by the user.
The following illustration shows the mathematical representation of the log() function.
The
mathmodule is required in order to use this function.
Syntax
log(number, base)
Parameters
This function requires two parameters:
- A
for which logarithm is to be calculated.number It must be greater than 0 - The
baseof the is an optional parameter.logarithm a number which must be greater than one
Return value
log() returns the logarithm of a number to the base sent as a parameter.
Remember that the
log()function calculates the natural logarithm of a number if the base is not provided, i.e.,log(y,e).
Code
#Module requiredimport math#number without a baseprint "log(20) : ", math.log(20)#number with a baseprint "log(2,2) : ", math.log(2,2)