Python Tutorial Python Advanced Python References Python Libraries

Python math - log() Function



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

Syntax

math.log(x, base)

Parameters

x Required. Specify the number.
base Optional. Specify the base. Default is e.

Return Value

Returns the natural logarithm or logarithm to the specified base of a given number.

Example:

In the example below, log() function is used to calculate the natural logarithm of a given number.

import math

print(math.log(10))
print(math.log(50))
print(math.log(100))

The output of the above code will be:

2.302585092994046
3.912023005428146
4.605170185988092

Example:

In this example, log() function is used to calculate the specified base logarithm of a given number.

import math

print(math.log(2, 2))
print(math.log(81, 3))
print(math.log(100, 10))

The output of the above code will be:

1.0
4.0
2.0

❮ Python Math Module