Tutorial

Python log() Functions to Calculate Logarithm

Published on August 4, 2022
author

Safa Mulani

Python log() Functions 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.


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

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Safa Mulani

author

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.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.