Natural Log in Python

  1. Calculate Natural Log of a Number With log() Function in Python
  2. Write Easy-To-Read Code Using the import Statement

This tutorial will introduce methods to calculate the natural log ln of a number in Python.

Calculate Natural Log of a Number With log() Function in Python

The log() function in the NumPy package returns the natural log of the number passed in the parameters. The natural log of a number has base e where e = 2.718. The following code example shows us how to calculate the natural log of a number using the log() function in Python.

import numpy

x = numpy.log(10)

print(x)

Output:

2.302585092994046

We calculate the natural log of 10 using the numpy.log() function in the above code. The above code works just fine. But, it is not reader-friendly. In mathematics, log denotes logarithm with base 10, and ln denotes natural logarithm with base e.

Write Easy-To-Read Code Using the import Statement

The import statement is used to import packages and libraries in our code. The following code example shows us how we can make our code more reader-friendly by using the import statement in Python.

from numpy import log as ln

x = ln(10)

print(x)

Output:

2.302585092994046

In the above code, we changed the log() function in the NumPy package to the ln() function using the import statement.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.