Python 3 – Number log10() Method
In Python 3, the log10() method is used to find out the logarithm base 10 value of a particular number. The log10() is a built-in method of Python 3, which we can use to calculate the logarithmic base 10 values of numbers. It takes one argument, which can be any number whose logarithm base 10 we want to find out.
Syntax:
The syntax of the log10() method is very easy to understand. It is as follows:
import math
math.log10(x)
Here x
is the number whose logarithm base 10 value we want to calculate. The math
library must be imported first to use this method. Otherwise, it will throw an error.
Return Value:
The log10()
method returns the logarithmic base 10 value of the given number. If the given number is less than or equals to zero, It raises a ValueError
exception.
Let’s see some examples to understand how the log10()
method works.
Example 1:
import math
x = 100
log10 = math.log10(x)
print("Logarithmic base 10 value of {} is: {}".format(x, log10))
Output:
Logarithmic base 10 value of 100 is: 2.0
In this example, we have imported the math
library, assigned the value 100
to the variable x
, and then calculated the logarithmic base 10 value of x
using the log10()
method. We then print out the result.
Example 2:
import math
x = 12.3456789
log10 = math.log10(x)
print("Logarithmic base 10 value of {} is: {}".format(x, log10))
Output:
Logarithmic base 10 value of 12.3456789 is: 1.0915149771692707
In this example, we have used a floating-point number 12.3456789
as the argument of the log10()
method to calculate its logarithmic base 10 value.
Example 3:
import math
x = 0
log10 = math.log10(x)
print("Logarithmic base 10 value of {} is: {}".format(x, log10))
Output:
ValueError: math domain error
In this example, we tried to calculate the logarithmic base 10 value of the number 0
using the log10()
method. Since the logarithmic value of 0
does not exist, it throws a ValueError
exception. Therefore, we should avoid using the log10()
method for numbers less than or equals to 0
.
Example 4:
import math
x = -10
log10 = math.log10(x)
print("Logarithmic base 10 value of {} is: {}".format(x, log10))
Output:
ValueError: math domain error
In this example, we tried to calculate the logarithmic base 10 value of a negative number -10
using the log10()
method. Since the logarithmic value of a negative number does not exist, it throws a ValueError
exception. Therefore, we should avoid using the log10()
method for negative numbers.
Conclusion:
The log10()
method is used to calculate the logarithmic base 10 value of a number. However, it can raise an exception if we use it for numbers less than or equals to 0
or negative numbers. Therefore, we should always use the log10()
method with caution and only for positive numbers that are greater than 0
.