In the previous article, we have discussed Python Program for expm1() Function
log() Function in Python:
The math.log() method returns the natural logarithm of a number or the base logarithm of a number.
Syntax:
math.log(x, base)
Parameters:
x: This is required. It is a number. It 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.
base: This is optional. The logarithmic base that should be used. ‘e’ is the default.
Return Value:
Returns a float value corresponding to the natural logarithm of a number or the logarithm of a number to base.
- The log function returns the output if the number argument is positive.
- If the number is negative or zero, the log function throws a ValueError.
- If it is not a number, the log function throws a TypeError.
Examples:
Example1:
Input:
Given first number = 1 Given second number = 6
Output:
The result after applying log() function on above given first number 1 = 0.0 The result after applying log() function on above given second number 6 = 1.791759469228055
Example2:
Input:
Given first number = 7 Given second number = 9
Output:
The result after applying log() function on above given first number 7 = 1.9459101490553132 The result after applying log() function on above given second number 9 = 2.1972245773362196
Program for log() Function 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 first number as static input and store it in another variable.
- Apply math.log() function to the given first number to get the natural logarithm or the base logarithm of a given number.
- Store it in another variable.
- Print the above result.
- Similarly, do the same for the other number.
- Apply the math.log() function to the given list element and print it.
- The Exit of 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 = [10, 5, 20, 25, 12] # Give the first number as static input and store it in another variable. gvn_numb1 = 1 # Apply math.log() function to the given first number to get the natural logarithm # or the base logarithm of a given number. # Store it in another variable. fnl_rslt = math.log(gvn_numb1) print("The result after applying log() function on above given first number", gvn_numb1, " = ", fnl_rslt) # similarly do the same for the other number. gvn_numb2 = 6 print("The result after applying log() function on above given second number", gvn_numb2, " = ", math.log(gvn_numb2)) # Apply math.log() function to the given list element and print it. print( "The result after applying log() function on given list element gvnlst[1] = ", math.log(gvn_lst[1]))
Output:
The result after applying log() function on above given first number 1 = 0.0 The result after applying log() function on above given second number 6 = 1.791759469228055 The result after applying log() function on given list element gvnlst[1] = 1.6094379124341003
Method #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 first number as user input using the float(input()) function and store it in a variable.
- Apply math.log() function to the given first number to get the natural logarithm or the base logarithm of a given number.
- Store it in another variable.
- Print the above result.
- Similarly, do the same for the other number.
- Apply the math.log() function to the given list element and print it.
- The Exit of 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(float, input( 'Enter some random List Elements separated by spaces = ').split())) # Give the first number as user input using the float(input()) function # and store it in a variable. gvn_numb1 = float(input("Enter some random number = ")) # Apply math.log() function to the given first number to get the natural logarithm # or the base logarithm of a given number. # Store it in another variable. fnl_rslt = math.log(gvn_numb1) print("The result after applying log() function on above given first number", gvn_numb1, " = ", fnl_rslt) # similarly do the same for the other number. gvn_numb2 = float(input("Enter some random number = ")) print("The result after applying log() function on above given second number", gvn_numb2, " = ", math.log(gvn_numb2)) # Apply math.log() function to the given list element and print it. print( "The result after applying log() function on given list element gvnlst[2] = ", math.log(gvn_lst[2]))
Output:
Enter some random List Elements separated by spaces = 4 5 3 1 2 Enter some random number = 7 The result after applying log() function on above given first number 7.0 = 1.9459101490553132 Enter some random number = 9 The result after applying log() function on above given second number 9.0 = 2.1972245773362196 The result after applying log() function on given list element gvnlst[2] = 1.0986122886681098
Know all about Mathematical Functions in Python by going through our Python Mathematical Methods Examples well explained step by step.