The Python log function calculates the logarithmic value of a given number with base E. BY default, it calculates the natural logarithmic value. However, we can specify the base value to find the nth base logarithmic value of a given number. The syntax of the math log Function is shown below.
math.log(number, base);
The base argument is an optional one. If you omit this, the Python log function considers default E as a logarithm base. However, you can change it using this parameter.
Number: A valid numerical expression.
- If the number argument is a positive number, it returns the output.
- If the number is Negative or Zero, it returns ValueError.
- And if it is not a number, it returns TypeError.
Python math log Function Example
The log Function calculates the logarithmic value of the given number with base E. Here, we used this method to find the logarithmic value of different data types.
import math
Tup = (1, 2, 3, -4 , 5) # Tuple Declaration
Lis = [-1, 2, -3.5, -4 , 5] # List Declaration
print('Logarithm value of Positive Number = %.2f' %math.log(1))
print('Logarithm value of Positive Decimal = %.2f' %math.log(2.5))
print('Logarithm value of Tuple Item = %.2f' %math.log(Tup[2]))
print('Logarithm value of List Item = %.2f' %math.log(Lis[4]))
print('Logarithm value of Multiple Number = %.2f' %math.log(2 + 7 - 5))
print('Logarithm value of String Number = ', math.log('Hello'))

- Within the first two statements, we used the math log Function directly on Positive integer and Decimal values.
- Next two statements, We used the math Function on Tuple and List items. If you observe the above Python screenshot, this Math function is working perfectly on them.
- We tried it directly on multiple values.
- We tried the logarithmic Function on the String value, and it returns TypeError: a float is required.
- Next, We tried it on a Negative value, and it is returning ValueError: math domain error.
- Last, We tried it on Zero value. As we said before, this is returning ValueError: math domain error.
How to use math.log with a custom base in Python?
If you check the syntax of the mth.log() function, it has an optional second parameter called base. It means we can use the log() function with a custom base or without to find the natural logarithm value.
In the following example, we use the math.log() function to find the logarithmic values of bases 2, 10, and 3. For base 2 and base 10, use dedicated log2() and log10() functions.
import math
print(math.log(16, 2))
print(math.log(100, 10))
print(math.log(90, 3))
4.0
2.0
4.095903274289384
Python log function with negative values
The built-in math.log() function does not allow negative values as parameters. If you pass a negative parameter value, the log() function raises a ValueError.
import math
print(math.log(-10))
ValueError: math domain error
If you use a positive first argument and a negative base value, the log() function raises the same ValueError.
Fixing log function ValueError
The cmath library has a log () function, and we can use this function to find the logarithmic value of negative numbers.
import cmath
print(cmath.log(-1))
3.141592653589793j
Python log function list comprehension
Similar to the regular numbers, we can use the log() function on a list of numbers. However, we must use a map function or a list comprehension to iterate over the list items and apply the math.log () function to each list item.
In the following example, we declared a list of five numeric values. Next, with the help of list comprehension, we applied the log() function to each list item.
import math
numbers = [1, 16, 55, 48, 100]
result = [math.log(x) for x in numbers]
print(result)
[0.0, 2.772588722239781, 4.007333185232471, 3.871201010907891, 4.605170185988092]
Similar to the above, we can find the logarithmic value with a custom base in the list items.
import math
numbers = [1, 16, 32, 64, 128]
result = [math.log(x, 2) for x in numbers]
print(result)
[0.0, 4.0, 5.0, 6.0, 7.0]