How To Calculate The Natural Logarithm (ln) In Python?

The natural logarithm of a number can be calculated by using different modules in Python. The numpy module provides the log() method in order to calculate the natural logarithm. Also, the Pytyhon math library provides the log() method in order to calculate natural logarithm too.

Calculate Natural Logarithm with math.log()

The math module is provided by the Python framework by default. The log() method of the math module can be used to calculate the natural logarithm of the specified number. In order to use the math.log() method the math module should be imported.

import math

math.log(1)
# 0.0

math.log(10)
# 2.302585092994046

math.log(100)
# 4.605170185988092

We can also import the math module log() method directly and call this method without module definition like below.

from math import log

log(1)
# 0.0

log(10)
# 2.302585092994046

log(100)
# 4.605170185988092

Calculate Natural Logarithm with numpy.log()

As a popular mathematical module, the numpy provides the log() method in order to calculate the natural log of the specified number. The numpy is a 3rd party module and not provided by Python by default so it should be installed with pip like below.

$ pip install numy

Now we can use the numpy.log() method like below.

import numpy

numpy.log(1)
# 0.0

numpy.log(10)
# 2.302585092994046

numpy.log(100)
# 4.605170185988092

The NumPy module definition can be shortened as “np” and the log() method can be used like below.

import numpy as np

np.log(1)
# 0.0

np.log(10)
# 2.302585092994046

np.log(100)
# 4.605170185988092

Leave a Comment