Log10 in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

Python provides math library which can be used to solve complex problems. The Math Library is rich with several mathematical functions like math.gamma(), math.asin(), math.ceil(), etc. In this article, we will study the log10() in Python.

Python log10 Function

The log10() function is used to convert an input into a base 10 logarithmic (it means we are expressing numbers in terms of what power of 10 produces them), which is later returned as a result or output. The log10() in Python is in the math library. It follows the condition that the input is greater than 0. Let's see an example to understand better the concept of the log10() in Python.

Example 1:

Code:

Output:

Explanation:

In the above example, we can see for using log10 in Python. We need to import the math library. After that, we take a numeric value, which will work as an input parameter for our log10. Finally, we call the math.log10() function using the print statement, passing the numeric input value a, and it returns the logarithmic base 10 value of a variable that is 1.38 as an output.

Example 2:

Code:

Output:

Syntax of Python log10 Function

In Python, as we all know, log10 is a Python Standard Library math inbuilt function. Therefore, first, we will start with math and then write the function. This function takes a numeric value as the input parameter. Therefore, the syntax of the log10() function in Python is:

Parameters of Python log10 Function

The log10() function in Python takes a single parameter, which is the number whose base 10 logarithm you want to calculate. This parameter must be a numeric value (integer or floating-point number), and it must be greater than 0. If the parameter is not a valid number or less than or equal to 0, the function will raise a ValueError exception.

Return Value of Python log10 Function

The log10() function in Python returns the base 10 logarithm of the input number as a floating-point value. If the input number is not a valid numeric value or less than or equal to 0, the function will raise a ValueError exception.

Exceptions of Python log10 Function

log10 is an inbuilt function, so it has its limitations. It works on particular inputs only, i.e.,

  • The function returns the output if the number input is a positive integer.

Output:

  • A ValueError is returned if the number parameter is zero or a negative value.

Output:

  • Additionally, it returns a TypeError if it is not an integer.

Output:

Working of log10 Function in Python

The log10 function is used to convert any numeric value to the logarithmic of base 10. The work of the log10 in Python is as follows:

  • We can see that to use the log10 in Python, we need to import the math library.
  • After that, we take a numeric value, which will work as an input parameter for our log10 in Python.
  • Finally, we refer to math.log10() function in Python uses print statements to pass the input numeric value, and it returns the logarithmic base 10 value of the variable.

Working is given below:

Working of  log10

This is how you can calculate the base 10 logarithmic value of any numeric value using the log10 in Python.

Uses of log10 Function

  • The log10 in Python is used to calculate the base-10 logarithmic value of any number.
  • Some examples of this include sound (decibel measures), earthquakes (Richter scale), the brightness of stars, and chemistry (pH balance, a measure of acidity and alkalinity).

Python log10 Function Examples

Example 1:

Code:

Output:

Example 2:

Code:

Output:

Example 3:

Code:

Output:

In the above code, we are trying to find the log of a string(Scaler). Our code is throwing a TypeError because the number should not be a string but a real number.

The Python math module has various functions that are used to solve mathematical operations. Some of these functions are given below, so take a look at them:

Conclusion

In this article, we have learned that :

  • Log10 is a Python standard library math inbuilt function 
  • The Log10 function is used to convert any numeric value into its base 10 logarithmic values 
  • Log10 can give TypeError and ValueError if the appropriate parameter is not passed.
  • We must import the math library to use the log10 inbuilt function.
  • Log10 has many similar inbuilt functions like log, log2, log1p, etc.