The math.log2() function calculates the base-2 logarithm of a number.

Syntax:
math.log2(x)
copy

Where x is the number whose base-2 logarithm we want.

ExampleEdit & Run
import math

print(math.log2(4))
print(math.log2(8))
print(math.log2(64))
print(math.log2(100))
copy
Output:
2.0 3.0 6.0 6.643856189774724 [Finished in 0.010720515623688698s]

The math.log2 function is shorthand  for math.log() function with 2 as the base i.e math.log(x, 2)

ExampleEdit & Run
import math

print(math.log2(50))
print(math.log(50, 2))
copy
Output:
5.643856189774724 5.643856189774724 [Finished in 0.010558011941611767s]

ValueError is raised if the value given as argument is negative.

ExampleEdit & Run

import math

math.log2(-1)
copy

Output:
Traceback (most recent call last):   File "<string>", line 3, in <module> ValueError: math domain error [Finished in 0.01057086419314146s]