Python math.log() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math module

Definition

math.log(x) returns the natural logarithm (base e) of positive number x. With an optional second argument, math.log(x, base) computes the logarithm in that base as math.log(x) / math.log(base).

For base 2 or base 10, prefer math.log2() and math.log10(), which are computed directly and avoid rounding quirks like math.log(1000, 10) returning 2.9999999999999996.

math.log() is a function in Python's standard math module: import it with import math and call math.log(...), or import the name directly with from math import log.

Syntax

Pass one positive number for natural log, or add a positive base != 1.

python
import math

ln_val = math.log(x)
log_b = math.log(x, base)
CallWhat it does
math.log(1)Returns 0.0.
math.log(math.e)Returns 1.0.
math.log(8, 2)Returns 3.0.
math.log(0)Raises ValueError: argument must be > 0.

Passing base=1 raises ZeroDivisionError because math.log(1) is 0.0.

Function signatures
python
math.log(x, [base=math.e])
# two-arg form divides log(x) by log(base)

Parameters

ParameterDescription
xRequired positive number (int or float). Zero or negative values raise ValueError.
baseOptional positive number not equal to 1. Defaults to math.e.

Return Value

Return ValueDescription
floatLogarithm of x. Returns inf for inf and nan for nan.

Python Examples

Example 1 — Estimate Investment Doubling Time in Years

Compute continuous doubling time math.log(2) / rate across interest rates.

python
import math

for pct in [4, 6, 8, 12]:
    rate = pct / 100.0
    years = math.log(2) / rate
    print(f"{pct:>2}% rate -> {years:.2f} years")

How It Works

  1. Continuous compounding rule. Solving e**(r*t) = 2 gives exact doubling time ln(2) / r.
  2. Convert percentage. Dividing each integer percentage by 100.0 turns it into a decimal rate.
  3. Formatted comparison. Two decimal places illustrate how doubling time drops faster than linearly as rates climb.

Example 2 — Use Custom Bases and Spot the log(1000, 10) Quirk

Evaluate logarithms in bases 2, 3, and 10, comparing math.log(1000, 10) against math.log10().

python
import math

print(math.log(8, 2))
print(round(math.log(81, 3), 1))
print(math.log(1000, 10) == 3.0)
print(math.log10(1000) == 3.0)

How It Works

  1. Exact base 2 division. Dividing ln(8) by ln(2) evaluates cleanly to 3.0.
  2. Arbitrary base. Base 3 of 81 rounds cleanly to 4.0.
  3. Dedicated log10 wins. Division ln(1000) / ln(10) loses one ULP below 3.0, whereas math.log10(1000) returns exact 3.0.

Example 3 — Catch ValueError and ZeroDivisionError on Invalid Calls

Handle zero input, negative input, and base 1 safely alongside infinity.

python
import math

for bad_x in [0, -5]:
    try:
        math.log(bad_x)
    except ValueError:
        print(f"log({bad_x}) raises ValueError")

try:
    math.log(10, 1)
except ZeroDivisionError:
    print("base 1 divides by log(1) == 0")

print(math.log(float("inf")))

How It Works

  1. Positive inputs only. Zero and negative numbers have no real logarithm, raising ValueError.
  2. Base one fails differently. Because math.log(1) equals zero, using base 1 triggers ZeroDivisionError.
  3. Infinity grows without bound. Positive infinity returns inf.

Frequently Asked Questions

Base e (Euler's number, ~2.71828), giving the natural logarithm. Pass a second argument for another base.
math.log(x, 10) divides two approximations (log(x) / log(10)) and returns 2.9999999999999996 for 1000, whereas math.log10(1000) returns exact 3.0.
Python computes log(x, base) as log(x) / log(base). Since log(1) is 0.0, dividing by it raises ZeroDivisionError.
Use cmath.log() from the cmath module, which returns a complex result.
No. Both x and base are positional-only parameters, so math.log(x=10) raises TypeError.

Next: math.log10()

Return the base-10 logarithm of x as a float.

log10() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful