
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Log Functions in Python
In this tutorial, we are going to learn about the logarithmic functions from math module. We have four variants of logarithmic functions. Pythons' provides all of them in the math module. Let's learn about them one by one.
math.log(number, [Base])
The math.log(number, [Base]) method is used to compute the logarithm of any Base. If we didn't specify any base value, then it will take e as default base.
Note − You will get ValueError if you pass a negative number to the method.
Example
Let's see some examples.
# importing math module import math # logarithm with base 3 print(math.log(15, 7))
Output
If you run the above program, you will get the following result.
1.3916625094004957
You can specify any base value you want in the above program. Let's see the same example without any base value. Default base value is e.
Example
# importing math module import math # logarithm with base e(default) print(math.log(15))
Output
If you run the above code, you will get the following results.
2.70805020110221
Example
Let's see what happens if we pass negative number to the math.log() method.
# importing math module import math # logarithm with negative number print(math.log(-15))
Output
If you run the above program, you will get the following results.
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-6-b686fcb806c6> in <module> 3 4 # logarithm with base e(default) ----> 5 print(math.log(-15)) ValueError: math domain error
math.log2(number)
If you want to compute logarithm for base 2 value, then we can use math.log2() method. It's similar to the above method. Let's see some examples.
Example
# importing math module import math # logarithm with base 2 print(math.log2(15))
Output
If you run the above code, you will get the following results.
3.9068905956085187
Similar to the math.log method, we will get an error if we pass a negative number to the math.log2 method. Let's see it with the example.
Example
# importing math module import math # logarithm with base 2 & negative number print(math.log2(-15))
Output
If you see the output of the program by executing it, then you will find that the error we got now and before is the same.
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-3-8019b45e571f> in <module> 3 4 # logarithm with base 2 & negative number ----> 5 print(math.log2(-15)) ValueError: math domain error
math.log10(number)
We can find the logarithm with base 10 using the math.log10 method. It's similar to the above math.log2 method. Let's see some examples.
Example
# importing math module import math # logarithm with base 10 print(math.log10(15))
Output
If you execute the above program, you will get the following output.
1.1760912590556813
Try to pass a negative number to the math.log10 method. You will get an error similar to the above methods.
Example
# importing math module import math # logarithm with base 10 & negative number print(math.log10(-15))
Output
If you see the output you will get the following error.
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-5-52ac56b802ca> in <module> 3 4 # logarithm with base 10 & negative number ----> 5 print(math.log10(-15)) ValueError: math domain error
math.log1p(number)
The method math.log1p(x) will compute log(1 + x) with base e. It computes logarithm of given number by adding 1 to it. Let's see some examples.
Example
# importing math module import math # logarithm print(math.log1p(15)) # similar to math.log(16)
Output
If you execute the above program, you will get the following results.
2.772588722239781
Try to pass a negative number to the math.log1p method. I'm sure that you will get an error as we have seen before.
Example
# importing math module import math # logarithm print(math.log1p(-15))
# importing math module import math # logarithm print(math.log1p(-15))
Output
We'll get the following error because of the negative number that we passed to the method.
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-15-26016884cb23> in <module> 3 4 # logarithm ----> 5 print(math.log1p(-15)) ValueError: math domain error
Conclusion
We've seen a total of four logarithm methods from the math module. We'll get an error if we pass negative number to any of the logarithmic methods seen in the tutorial. And you can pass floating numbers to the methods as well. Try to execute the examples seen in this tutorial with floating numbers.