The Python log2 math function is used to calculate the logarithmic value of a given number of base 2. In this section, we discuss how to use the math log2 function in this Programming language with an example.
The syntax of the log2 Function in Python Programming language is as shown below.
math.log2(number);
- If the number argument is a positive number, the function returns the output.
- If the number argument is a Negative number or Zero, it returns ValueError.
- And if it is not a number, it returns TypeError.
Python log2 Function Example
The log2 Function calculates the logarithmic value of a given number of base 2. In this example, We are going to check the base 2 logarithmic value with different data types and display the output.
TIP: The math.log2 function is more accurate than math.log(x, 2). Please refer to the math.log() function article to understand the log (natural logarithmic value) function.
import math
Tup = (10, 20, 30, -40 , 50) # Tuple Declaration
Lis = [-1, 2, 3.5, -43 , 50] # List Declaration
print('Logarithm value of Positive Number = %.2f' %math.log2(5))
print('Logarithm value of Positive Decimal = %.2f' %math.log2(2.5))
print('Logarithm value of Tuple Item = %.2f' %math.log2(Tup[2]))
print('Logarithm value of List Item = %.2f' %math.log2(Lis[2]))
print('Logarithm value of Multiple Number = %.2f' %math.log2(2 + 7 - 5))
print('Logarithm value of String Value = ', math.log2('Python'))

- Within the first two statements, We used it directly on Positive integer and Decimal values.
- Next two statements, We used the log2 Function on Tuple and List items. If you observe the above Python screenshot, this Math function is calculating the logarithm value of base 2.
- Next statement, We tried Python log2 Function with multiple values
- Next, We tried on the String value, and it returns TypeError: a float is required
- Here, We tried on Zero value. As we said before, this is returning ValueError: math domain error.
- Last, We tried on Negative value. As we said before, it returns ValueError: math domain error.
What is the difference between math.log(x, 2) and math.log2(x) in Python?
In short, both return the same result. The math.log2(x) function is a direct approach to finding the base 2 logarithmic value of a given number. On the other hand, the math.log() function uses the custom base 2 to find the logarithm value. Also, check the log1p and log10 functions.
In most cases, both math.log(x, 2) and math.log2(x) returns the same output. However, in some cases, there will be a minute difference in the decimal precision value between the two.
import math
x = 10
print(math.log(x, 2))
print(math.log2(x))
3.3219280948873626
3.321928094887362
How to get the integer part of log base 2 in Python?
By default, it returns a floating-point number. There are multiple approaches to get the integer part from the log2() function result. They are using the floor function, ceil method, the int method, or the bit_length.
In the following example, we declared an integer (10) and used the log2() function to find the base 2 logarithmic value of 10. Next, we applied floor(), int(), and bit_length to get the integer part.
NOTE: When we use the ceil() function, instead of returning the integer part, it returns the next closest inetegr value.
import math
x = 10
y = math.log2(x)
print(y)
print(math.floor(y))
print(int(y))
print(x.bit_length() - 1)
3.321928094887362
3
3
3
NOTE: The bit_length property is an alternative to the built-in log2() function.