// Tutorial //

Python log() Functions to Calculate Logarithm

Published on August 3, 2022
Default avatar
By Safa Mulani
Developer and author at DigitalOcean.
Python log() Functions to Calculate Logarithm

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.


Understanding the log() functions in Python

In order to use the functionalities of Log functions, we need to import the math module using the below statement.

import math

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.

Syntax:

math.log(x)

The 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.

Example:

import math   

print("Log value: ", math.log(2))

In the above snippet of code, we are requesting the logarithmic value of 2.

Output:

Log value:  0.6931471805599453

Variants of Python log() Functions

The following are the variants of the basic log function in Python:

  • log2(x)
  • log(x, Base)
  • log10(x)
  • log1p(x)

1. log2(x) - log base 2

The math.log2(x) function is used to calculate the logarithmic value of a numeric expression of base 2.

Syntax:

math.log2(numeric expression)

Example:

import math 

print ("Log value for base 2: ") 
print (math.log2(20)) 

Output:

Log value for base 2: 
4.321928094887363

2. log(n, Base) - log base n

The math.log(x,Base) function calculates the logarithmic value of x i.e. numeric expression for a particular (desired) base value.

Syntax:

math.log(numeric_expression,base_value)

This function accepts two arguments:

  • numeric expression
  • Base value

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.

Example:

import math 

print ("Log value for base 4 : ") 
print (math.log(20,4)) 

Output:

Log value for base 4 : 
2.1609640474436813

3. log10(x) - log base 10

The math.log10(x) function calculates the logarithmic value of the numeric expression to the base 10.

Syntax:

math.log10(numeric_expression)

Example:

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.

Output:

Log value for base 10 : 
1.1760912590556813

4. log1p(x)

The 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)

Syntax:

math.log1p(numeric_expression)

Example:

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.

Thus, math.log1p(15) is equivalent to math.log(16).

Output:

Log value(1+15) for x = 15 is: 
2.772588722239781

Understanding log in Python NumPy

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.

import numpy

Syntax:

numpy.log(input_array)

The numpy.log() function accepts input array as a parameter and returns the array with the logarithmic value of elements in it.

Example:

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) 

Output:

Array input elements:
 [10, 20, 30, 40, 50]
Resultant array elements:
 [ 2.30258509  2.99573227  3.40119738  3.68887945  3.91202301]

Conclusion

In this article, we have understood the working of Python Log functions and have unveiled the variants of the logarithmic function in Python.


References


Want to learn more? Join the DigitalOcean Community!

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
About the authors
Default avatar
Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?