Python math.log()

ishg-153's avatar
Published May 19, 2021Updated Mar 17, 2024
Contribute to Docs

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

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

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.

Example

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

import math
print(math.log(12))

The above code gives the following output:

2.4849066497880004

Codebyte Example

Run the following example that uses the math.log() function to understand its working:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours