Python log() Function to Calculate Logarithm

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.

log() Function to Calculate Logarithm – Understanding the log() function

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.

Syntax:

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

log() Function to Calculate Logarithm – Variants

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.

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.

math.log(numeric_expression,base_value)

This function accepts two arguments:

  • numeric expression
  • Base value

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.

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

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.

Syntax:

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 – log() Function to Calculate Logarithm

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

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: