EN
Python - math.log() method example
0 points
The math.log()
method returns the natural logarithm (base e) of a number.
xxxxxxxxxx
1
import math
2
3
print(math.log(1)) # 0
4
print(math.log(7)) # 1.9459101490553132
5
print(math.log(10)) # 2.3025850929940460
6
print(math.log(100)) # 4.6051701859880920
7
print(math.log(1000)) # 6.9077552789821370
8
9
print(math.e) # 2.718281828459045
10
11
# Logarithm with custom base is placed in the below example.
The math.log()
method is presented on the following chart:

Syntax |
xxxxxxxxxx 1 math.log(x, newBase) |
Parameters |
|
Result |
If If If |
Description |
|
This example shows a logarithmic function calculation with its own base.
xxxxxxxxxx
1
import math
2
3
4
def calculate_logarithm(log_base, x):
5
a = math.log(x)
6
b = math.log(log_base)
7
return a / b
8
9
10
# Logarithm with custom base:
11
# base x y
12
print(calculate_logarithm( 2, 2 ) ) # 1.0
13
print(calculate_logarithm( 2, 4 ) ) # 2.0
14
print(calculate_logarithm( math.e, math.e ) ) # 1.0
15
print(calculate_logarithm( 3, 9 ) ) # 2.0
16
print(calculate_logarithm( 3, 81 ) ) # 4.0
17
print(calculate_logarithm( 10, 10 ) ) # 1.0