math.log10() Method in Python:
The math.log10() method returns a number’s base-10 logarithm.
Syntax:
math.log10(x)
Parameters
x: This is Required. Specifies the value for which the logarithm should be calculated.
- If the value is 0 or a negative number, a ValueError is returned.
- If the value is not a number, a TypeError is returned.
Return Value:
Returns a float value that represents a number’s base-10 logarithm.
Examples:
Example1:
Input:
Given Number = 4 Given List = [2, 3.5, 4, 5.62, 1]
Output:
The base-10 logarithm of a given number { 4 } = 0.6020599913279624
The base-10 logarithm of a list element gvn_lst[1] = 0.5440680443502757Example2:
Input:
Given Number = 2 Given List = [8, 2, 9, 1, 0]
Output:
The base-10 logarithm of a given number { 2.0 } = 0.3010299956639812
The base-10 logarithm of a list element gvn_lst[2] = 0.9542425094393249math.log10() Method with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Import math module using the import keyword.
- Give the list as static input and store it in a variable.
- Give the number as static input and store it in another variable.
- Apply math.log10() method to the given number to get base-10 logarithm of a given number.
- Store it in another variable.
- Print the above result.
- Apply the math.log10() method to the given list to get base-10 logarithm of a list element.
- Store it in another variable.
- Print the base-10 logarithm of a given list element.
- The Exit of the Program.
Below is the implementation:
# Import math module using the import keyword.
import math
# Give the list as static input and store it in a variable.
gvn_lst = [2, 3.5, 4, 5.62, 1]
# Give the number as static input and store it in another variable.
gvn_numb = 4
# Apply math.log10() method to the given number to get base-10 logarithm of a
# given number.
# Store it in another variable.
rslt1 = math.log10(gvn_numb)
# Print the above result.
print("The base-10 logarithm of a given number {", gvn_numb, "} =", rslt1)
# Apply math.log10() method to the given list to get base-10 logarithm of a list
# element.
# Store it in another variable.
rslt2 = math.log10(gvn_lst[1])
# Print the base-10 logarithm of a given list element.
print("The base-10 logarithm of a list element gvn_lst[1] =", rslt2)
Output:
The base-10 logarithm of a given number { 4 } = 0.6020599913279624
The base-10 logarithm of a list element gvn_lst[1] = 0.5440680443502757Method #2: Using Built-in Functions (User Input)
Approach:
- Import math module using the import keyword.
- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in a variable.
- Give the number as user input using the float(input()) function and store it in another variable.
- Apply math.log10() method to the given number to get base-10 logarithm of a given number.
- Store it in another variable.
- Print the above result.
- Apply the math.log10() method to the given list to get base-10 logarithm of a list element.
- Store it in another variable.
- Print the base-10 logarithm of a given list element.
- The Exit of the Program.
Below is the implementation:
# Import math module using the import keyword.
import math
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
'Enter some random List Elements separated by spaces = ').split()))
# Give the number as user input using the float(input()) function and
# store it in another variable.
gvn_numb = float(input("Enter some random number = "))
# Apply math.log10() method to the given number to get base-10 logarithm of a
# given number.
# Store it in another variable.
rslt1 = math.log10(gvn_numb)
# Print the above result.
print("The base-10 logarithm of a given number {", gvn_numb, "} =", rslt1)
# Apply math.log10() method to the given list to get base-10 logarithm of a list
# element.
# Store it in another variable.
rslt2 = math.log10(gvn_lst[2])
# Print the base-10 logarithm of a given list element.
print("The base-10 logarithm of a list element gvn_lst[2] =", rslt2)
Output:
Enter some random List Elements separated by spaces = 8 2 9 1 0
Enter some random number = 2
The base-10 logarithm of a given number { 2.0 } = 0.3010299956639812
The base-10 logarithm of a list element gvn_lst[2] = 0.9542425094393249