Python log() Function

Python 数字Python Numbers


Description

log() returns the natural logarithm of x.


Syntax

The following is the syntax of the log() method:

import math

math.log(x[, base])

Note:log() is not directly accessible; you need to import the math module and call the method via a static object.


Parameters

  • x -- Numeric expression.
  • base -- Optional, base, defaults to e.

  • Return Value

    Returns the natural logarithm of x, where x>0.

    Example

    The following shows an example of using the log() method:

    Example (Python 2.0+)

    #!/usr/bin/python # -*- coding: UTF-8 -*- import math # Import the math module 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) # Set the base print "math.log(10,2) : ", math.log(10,2)

    The output 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
    

    Python 数字Python Numbers

    Other Extensions