The Python math log function calculates the logarithmic value of a given number with base E. The syntax of the Python 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 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 Python 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.