What is math.log() in Python?

Share

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.

Mathematical representation of the log() function

The math module is required in order to use this function.

Syntax

log(number, base)

Parameters

This function requires two parameters:

  • A numberIt must be greater than 0 for which logarithm is to be calculated.
  • The base of the logarithma number which must be greater than one is an optional parameter.

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 required
import math
#number without a base
print "log(20) : ", math.log(20)
#number with a base
print "log(2,2) : ", math.log(2,2)