Python math log() function is used to calculate the natural logarithm of a number or the logarithm of a number to base. The math log() function inbuilt logarithmic functions under the module “math” which allows us to compute logs using a single line.
math.log(n, base)
The base
is optional and it’s the logarithmic base to use. The default is e. If you want to use a function from a package, you have several options to do it. The two most used:
from math import log
This will make log
available in the namespace and you can use it directly.
import math
This will make math
available and you can access the function as math.log()
.
Python math log function example
Simple example code use math.log()
to return the natural log of 12
(base e):
import math
# base e
print(math.log(12))
# base 5
print(math.log(12, 5))
Output:
Do comment if you have any doubts or suggestions on this Python math module topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.