Python math.log10() Method
- Syntax
- Example 1: Base-10 Logarithm of a Number
- Example 2: If Input Is Not a Number
-
Example 3: If Input Less Than or Equal to
0 - Example 4: If Input Is Infinity
-
Example 5: If Input Is
NaN
The Python programming language offers a library, math, that contains implementation for various mathematical operations such as trigonometric functions and logarithmic functions.
Logarithm refers to the inverse function of exponentiation. Simply put, the logarithm of a number a is the exponent or number (x) to which another number, the base b, should be raised to make that number a.

In this article, we will discuss a method, log10(), available in the math module that computes the logarithm of a number with base 10.
Syntax
math.log10(x)
Parameters
| Type | Description | |
|---|---|---|
x |
Float | A non-negative integer and non-zero value. |
Return
The log10() method returns the logarithm to the base of 10 for a number. It is equivalent to math.log(x, 10) and, in some cases, more accurate than it.
Example 1: Base-10 Logarithm of a Number
import math
print(math.log10(0.000001))
print(math.log10(1))
print(math.log10(0.341))
print(math.log10(99999))
print(math.log10(2352.579))
Output:
-6.0
0.0
-0.4672456210075022
4.999995657033466
3.3715442160261846
The Python code above computes the base-10 logarithm value for 0.000001, 1, 0.341, 99999, and 2352.579. Note that these values are non-negative and non-zero.
For inputs in the range (0, 1), the base-10 logarithm returns negative results. On the flip side, the range [1, ∞) yields positive results.
Example 2: If Input Is Not a Number
import math
print(math.log10("this is a string"))
Output:
Traceback (most recent call last):
File "main.py", line 3, in <module>
print(math.log10("this is a string"))
TypeError: must be real number, not str
The Python code above takes a string as an input. Since the log10() method expects a numeric value such as an integer or a float, it raises a TypeError exception.
Example 3: If Input Less Than or Equal to 0
import math
print(math.log10(0))
Output:
Traceback (most recent call last):
File "main.py", line 3, in <module>
print(math.log10(0))
ValueError: math domain error
The Python code above takes 0 as input, and since 0 is out of the log10() method’s domain, it raises a ValueError exception.
import math
print(math.log10(-99.09))
Output:
Traceback (most recent call last):
File "main.py", line 3, in <module>
print(math.log10(-99.09))
ValueError: math domain error
The Python code above takes -99.09 as input which is less than 0. Such values are invalid for the log10() method because they lie outside its domain.
Hence, it raises a ValueError exception.
Example 4: If Input Is Infinity
import math
print(math.log10(math.inf))
Output:
inf
The logarithm of infinity to base 10 is infinity.
Example 5: If Input Is NaN
import math
print(math.log10(math.nan))
Output:
nan
