EN
Python - math.log() method example
0
points
The math.log()
method returns the natural logarithm (base e) of a number.
import math
print(math.log(1)) # 0
print(math.log(7)) # 1.9459101490553132
print(math.log(10)) # 2.3025850929940460
print(math.log(100)) # 4.6051701859880920
print(math.log(1000)) # 6.9077552789821370
print(math.e) # 2.718281828459045
# Logarithm with custom base is placed in the below example.
The math.log()
method is presented on the following chart:
1. Documentation
Syntax |
|
Parameters |
|
Result |
If If If |
Description |
|
2. Logarithm with custom base example
This example shows a logarithmic function calculation with its own base.
import math
def calculate_logarithm(log_base, x):
a = math.log(x)
b = math.log(log_base)
return a / b
# Logarithm with custom base:
# base x y
print(calculate_logarithm( 2, 2 ) ) # 1.0
print(calculate_logarithm( 2, 4 ) ) # 2.0
print(calculate_logarithm( math.e, math.e ) ) # 1.0
print(calculate_logarithm( 3, 9 ) ) # 2.0
print(calculate_logarithm( 3, 81 ) ) # 4.0
print(calculate_logarithm( 10, 10 ) ) # 1.0