Python log

The Python math log function calculates the logarithmic value of a given number with base E. The syntax of the 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'))
Python LOG Function
  1. Within the first two statements, we used the math log Function directly on Positive integer and Decimal values.
  2. 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.
  3. We tried it directly on multiple values.
  4. We tried the logarithmic Function on the String value, and it returns TypeError: a float is required.
  5. Next, We tried it on a Negative value, and it is returning ValueError: math domain error.
  6. Last, We tried it on Zero value. As we said before, this is returning ValueError: math domain error.