What is math.log2() in Python?

The log2() function in Python returns the logarithm of a number to the base 2.

Figure 1 shows the mathematical representation of the log2() function.

Figure 1: Mathematical representation of the log2() function

The math module is required for this function.

Syntax

log2(number)

Parameter

This function requires a number whose logarithm base 2 is to be calculated, and it must be greater than zero.

Return value

log2() returns the logarithm of a number to the base 2.

Example

#Module required
import math
#example
print ("log2(10) : ", math.log2(10))
print ("log2(2) : ", math.log2(2))
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources