Published May 19, 2021Updated Aug 7, 2023
Contribute to Docs

The math.log() function returns the natural logarithm of a number or the logarithm of a number to the given base.


math.log(n, base)

The math.log() function takes the following parameters:

  • n is a required number or numeric expression to calculate the logarithm.
  • base is an optional number to specify the logarithm base. The default value for the base is e.

The math.log() function returns a float value, the natural logarithm of n or the logarithm of n to base. If n is 0 or a negative number, it returns a ValueError.


Use math.log() to return the natural log of 12 (base e):

import math
# Output: 2.4849066497880004

Codebyte Example

The following example is runnable and uses the math.log() function to return the natural and base 10 logarithms of a number:


All contributors

Looking to contribute?

Learn Python on Codecademy