Python math.log2() Method

Python math 模块Python math Module

Python math.log2(x)The method returns the base-2 logarithm of x.

Syntax

The syntax of the math.log2() method is as follows:

math.log2(x)

Parameter Description:

  • x-- Required, a number. If x is not a number, returns TypeError. If the value is 0 or negative, returns ValueError.

Return Value

Returns a floating-point number (float), representing the base-2 natural logarithm of a number.

Example

The following example returns the base-2 natural logarithm of a number:

Example

# Import the math package
import math

# Output the base-2 natural logarithm of a number
print(math.log2(2.7183))
print(math.log2(2))
print(math.log2(1))

Output:

1.4427046851812222
1.0
0.0

Python math 模块Python math Module

Other Extensions