By Safa Mulani
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Logarithms are used to depict and represent large numbers. The log is an inverse of the exponent. This article will dive into the Python log() functions. The logarithmic functions of Python help the users to find the log of numbers in a much easier and efficient manner.
In order to use the functionalities of Log functions, we need to import the
math module using the below statement.
We all need to take note of the fact that the Python Log functions cannot be accessed directly. We need to use the
math module to access the log functions in the code.
math.log(x) function is used to calculate the natural logarithmic value i.e. log to the base e (Euler’s number) which is about 2.71828, of the parameter value (numeric expression), passed to it.
import math print("Log value: ", math.log(2))
In the above snippet of code, we are requesting the logarithmic value of 2.
Log value: 0.6931471805599453
The following are the variants of the basic log function in Python:
math.log2(x) function is used to calculate the logarithmic value of a numeric expression of base 2.
import math print ("Log value for base 2: ") print (math.log2(20))
Log value for base 2: 4.321928094887363
math.log(x,Base) function calculates the logarithmic value of x i.e. numeric expression for a particular (desired) base value.
This function accepts two arguments:
Note: If no base value is provided to the function, the math.log(x,(Base)) acts as a basic log function and calculates the log of the numeric expression to the base e.
import math print ("Log value for base 4 : ") print (math.log(20,4))
Log value for base 4 : 2.1609640474436813
math.log10(x) function calculates the logarithmic value of the numeric expression to the base 10.
import math print ("Log value for base 10: ") print (math.log10(15))
In the above snippet of code, the logarithmic value of 15 to the base 10 is calculated.
Log value for base 10 : 1.1760912590556813
math.log1p(x) function calculates the log(1+x) of a particular input value i.e. x
Note: math.log1p(1+x) is equivalent to math.log(x)
import math print ("Log value(1+15) for x = 15 is: ") print (math.log1p(15))
In the above snippet of code, the log value of (1+15) for the input expression 15 is calculated.
math.log1p(15) is equivalent to
Log value(1+15) for x = 15 is: 2.772588722239781
Python NumPy enables us to calculate the natural logarithmic values of the input NumPy array elements simultaneously.
In order to use the numpy.log() method, we need to import the NumPy module using the below statement.
numpy.log() function accepts input array as a parameter and returns the array with the logarithmic value of elements in it.
import numpy as np inp_arr = [10, 20, 30, 40, 50] print ("Array input elements:\n", inp_arr) res_arr = np.log(inp_arr) print ("Resultant array elements:\n", res_arr)
Array input elements: [10, 20, 30, 40, 50] Resultant array elements: [ 2.30258509 2.99573227 3.40119738 3.68887945 3.91202301]
In this article, we have understood the working of Python Log functions and have unveiled the variants of the logarithmic function in Python.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.Sign up now