The Python log2() method is one of the Python Math functions used to calculate the logarithmic value of a provided number of base 2.
Python log2()
Python log2(x) is a built-in function used to get the logarithm of any given number with base 2. The log2(x) function is under the math library, so we need to import the math library to use the log2() function.
Python math.log2() function is a library method of the math module, it is used to get the base-2 logarithm of a number, and it accepts the number and returns the base-2 logarithm of the given number.
Syntax
math.log2(number)
Parameters
The log2() function takes one argument.
number -> whose log we want to find with base 2.
Return Value
The log2() function returns the logarithm of base 2 of the specific given number. But this function throws a ValueError exception if any value is passed as an argument.
See the following scenarios of Python log2().
- If a numeric argument is a positive number, the log2 function returns the output.
- If a numeric argument is a Negative number or Zero, the log2 function returns ValueError.
- If it is not a number, log2() function returns TypeError.
Programming Example
See the following code.
# Importing math library import math # initializing values # positive value num = 10 print("Logarithm with base 2 of the value ", num, " is: ", math.log2(num)) # Negative number num = -10 print("Logarithm with base 2 of the value ", num, " is: ", math.log2(num))
Output
Logarithm with base 2 of the value 10 is: 3.321928094887362 Traceback (most recent call last): File "log2p1.py", line 12, in <module> print("Logarithm with base 2 of the value ",num," is: ",math.log2(num)) ValueError: math domain error
In this program, we have first initialized the value. We have calculated the logarithm of the number with base 2, and in the next line, we wanted to calculate the logarithm of a negative number, but as per rule, the program threw an exception of ValueError.
Example 2
# Importing math library import math # taking input from user values # positive value num = int(input("Enter a num to find log2(): ")) print("Logarithm with base 2 of the value ", num, " is: ", math.log2(num))
Output
Enter a num to find log(1+a): 10 Logarithm with base 2 of the value 10 is: 3.321928094887362
In this program, we have taken input from the user then we have calculated the logarithm of base 2.
The Python log2() method is more accurate than math.log(x, 2).
Python log2 with list and tuple
Python log2 method calculates the logarithmic value of the given number of base 2. In this code example, we will check the base 2 logarithmic value with different data types and display the output.
# app.py import math Tup = (11, 21, 30, -46, 52) # Tuple Declaration Lis = [-1, 2, 3.5, -43, -19] # List Declaration print('The log2() value of Positive Number = %.2f' % math.log2(5)) print('The log2() value of Positive Decimal = %.2f' % math.log2(2.5)) print('The log2() value of Tuple Item = %.2f' % math.log2(Tup[2])) print('The log2() value of List Item = %.2f' % math.log2(Lis[2])) print('The log2() value of Multiple Number = %.2f' % math.log2(2 + 7 - 5)) print('The log2() value of String Value = ', math.log2('Python'))
Output
python3 app.py The log2() value of Positive Number = 2.32 The log2() value of Positive Decimal = 1.32 The log2() value of Tuple Item = 4.91 The log2() value of List Item = 1.81 The log2() value of Multiple Number = 2.00 Traceback (most recent call last): File "app.py", line 13, in <module> print('The log2() value of String Value = ', math.log2('Python')) TypeError: must be real number, not str
That’s it for this tutorial.