Python 3 – Number log() Method
In Python, the logarithm of a number can be calculated using the log() method. The log() method is used to calculate the natural logarithm (base e) of a number. In this article, we will learn how to use the log() method in Python 3 with some examples.
Syntax of log() Method
import math
math.log(number, base)
Parameters of log() Method
The log() method accepts two parameters:
- number: The number for which we want to calculate the logarithm. The value of the number should be greater than 0.
- base (Optional): The base of the logarithm. The default value of the base is e.
Returns of log() Method
The log() method returns the natural logarithm (base e) of the number passed as a parameter.
Examples of using log() Method
Example 1: Calculating Natural Logarithm
In this example, we will calculate the natural logarithm of a number using the log() method.
import math
num = 5
result = math.log(num)
print("Natural Logarithm of", num, "is", result)
Output:
Natural Logarithm of 5 is 1.6094379124341003
Example 2: Calculating Logarithm with a Different Base
In this example, we will calculate the logarithm of a number with a base of 10 using the log() method.
import math
num = 100
base = 10
result = math.log(num, base)
print("Logarithm of", num, "with base", base, "is", result)
Output:
Logarithm of 100 with base 10 is 2.0
Logarithmic Identities
Here are some logarithmic identities that can be used to simplify logarithmic expressions:
- log(a * b) = log(a) + log(b)
- log(a / b) = log(a) – log(b)
- log(a^n) = n * log(a)
These identities can be used to simplify complex logarithmic expressions and make them easier to evaluate.
Conclusion
The log() method in Python 3 is used to calculate the natural logarithm of a number. It takes two parameters- the number for which we want to calculate the logarithm and the base of the logarithm (optional). The log() method returns the natural logarithm of the number passed as a parameter. Logarithmic identities can be used to simplify complex logarithmic expressions.