log() returns the natural logarithm of x. math.log(x) is equivalent to ln(x) in mathematics, x>0, find the logarithm with the base e, e = 2.718281828459; math.log10(x) is equivalent to lg(x) in mathematics, x>0, find the logarithm with base 10. The base can be set by log(x[, base]), for example, log(x, 10) represents the logarithm with base 10.
Syntax
The following is the syntax of the log() method:
import math math.log(x[, base])
Note: log() is If it cannot be accessed directly, you need to import the math module and call this method through a static object.
Parameters
x -- Numeric expression. base -- optional, base, default is e.
Related recommendations: "Python Video Tutorial"
Return value
Returns the natural logarithm of x, x>0 .
Example
The following shows an example of using the log() method:
#!/usr/bin/python # -*- coding: UTF-8 -*- import math # 导入 math 模块 print "math.log(100.12) : ", math.log(100.12) print "math.log(100.72) : ", math.log(100.72) print "math.log(119L) : ", math.log(119L) print "math.log(math.pi) : ", math.log(math.pi)# 设置底数 print "math.log(10,2) : ", math.log(10,2)
The output result after running the above example is:
math.log(100.12) : 4.60636946656 math.log(100.72) : 4.61234438974 math.log(119L) : 4.77912349311 math.log(math.pi) : 1.14472988585 math.log(10,2) : 3.32192809489
The above is the detailed content of How to express logarithmic function in python. For more information, please follow other related articles on the PHP Chinese website!