The log function of Python math module

The log() function of the Python math module returns the logarithm of a given number for a specified base. If the base parameter is omitted the function computes and returns the natural logarithm.

Math Updated 21 Jun 2022 1 min read

Overview

The function returns the logarithm of a given number for a given base. When base is not given the function uses e as the base and returns the natural logarithm.

Example

python
# Example Python program that uses

            # the log function to compute the logarithm

            # of numbers for various bases

            import math

num     = 1000000

            base     = 10

            logVal     = math.log(num, base)

            print("Logarithm of %d base %d : %d"%(num, base, logVal))

base     = 5

            logVal     = math.log(num, base)

            print("Logarithm of %d base %d : %d"%(num, base, logVal))

base     = 2

            logVal     = math.log(num, base)

            print("Logarithm of %d base %d : %d"%(num, base, logVal))

logVal     = math.log(num)

            print("Natural logarithm of %d : %d"%(num, logVal))

Output

Output
Logarithm of 1000000 base 10 : 5

Logarithm of 1000000 base 5 : 8

Logarithm of 1000000 base 2 : 19

Natural logarithm of 1000000 : 13