log2 Function in Python
Overview
The logarithm is the inverse function to exponentiation, in mathematics. According to that, the logarithm of a given integer x is the exponent to which any defined number of base be, must be **raised in order** to obtain that number x`. In this article, we will be learning about the log2 function in Python, in detail covering the important examples and use cases.
The log2 in Python is one of the Python functions, which is used to calculate the logarithmic value of any given number, of base 2.
log2 Function in Python
The function in Python is a built-in function, that is used to get the logarithm of a given number, raised to a base 2. The function is present under the math library in Python. Hence, before using the $log_2(x)$ function, we must ensure that the math library is imported. Please note that since it is built-in in Python, we need not install the function explicitly.
Takeaway: The Python math.log2() function is a library function of the math module that is used to obtain the base-2 logarithm of a given number. It takes a number as an input and returns the base-2 logarithm of the given number.
Syntax of the log2 in Python
To use tog2 in Python, we must import the math module, since this function is defined under the module. Let us look into the syntax of the same.
Parameters of the log2 in Python
The Log2Python takes one argument as input. The argument is the number x (say) whose log we want to find with base 2. It is a mandatory parameter and must be greater than 0.
Return Value of the log2 in Python
The log2 function in Python returns the logarithm raised to the base 2, of the number given as input. However, this function throws a ValueError exception if any negative value is passed to it as the argument.
Let us consider the below scenarios to cover the return values of the function in Python.
- If we pass a positive number as an argument to the function in Python, then the function returns the correct output.
- In case we pass a negative number as the argument, then the function raises a ValueError exception and returns the same.
- However, if we pass some non-numeric argument(not any number) then the function returns the TypeError exception.
Exceptions of the log2 in Python
There are basically 2 exceptions of the function in Python. We have already covered it earlier, but let us specifically look into them now --
- If we pass any negative value as an argument to the function in Python, then a ValueError exception is raised.
- If a non-numeric number is passed as an argument to the function in Python, then the TypeError exception is raised. Apart from that, the function in Python works well for other numeric inputs.
Working of the log2 in Python
The math.log2(x) function in Python was released in the Python 3.3 version. It basiturns the logarithm of a number** raised to the base 2. According to the Python documentation, it is considered more accurate than the log(x, 2) function, which is another in-built function for calculating the logarithms of numbers.
If the argument to the function in Python is invalid, such as negative or non-numeric, then it raises a ValueError or TypeError exception.
Uses of the log2 in Python
The function in Python has the following uses:
- The function is used for computing the logarithm of a number raised to the base 2.
- The function is considered even more accurate than the log(x, 2) in-built function in Python.
- The function is released in the Python 3.3 version and can be used by all the later versions.
log2 in Python Examples
Now that we have covered the function in Python in great detail, let us look into its code examples to understand it better practically.
Example 1 - Calculating Logarithm of Positive Number
In this example of log2 in python, we will be calculating the logarithm of a positive number, using the function in Python. Let us look into the code of the same.
Code:
Output:
Explanation: In the above example, we have computed the logarithm base 2 of a positive number using the function in Python. As expected, it returned us the accurate result for the same. Please note that it returns a float value of <class 'float'> as output (and not an integer).
Example 2 - Calculating the Logarithm of a Negative Number
In this example of log2 in python, we will be calculating the logarithm of a positive number, using the function in Python. Let us look into the code of the same.
Code:
Output:
Explanation: In the above example, we have computed the logarithm base 2 of a negative number using the function in Python. As expected, it returned us the ValueError: math domain error for the same. Hence, we should not pass a negative number as an input for the function in Python.
Example 3 - Calculating the Logarithm of a Non-numeric Value
In this example of log2 in python, we will be calculating the logarithm of a non-numeric value, using the function in Python. Let us look into the code of the same.
Code:
Output:
Explanation:
In the above example, we have computed the logarithm base 2 of a non-numeric value using the function in Python. As expected, it returned us the TypeError: must be real number, not str exception for the same. Hence, we should not pass a non-numeric value as an input for the function in Python.
Example 4 - Calculating Logarithm of Decimal Numbers
In this example of log2 in python, we will be calculating the logarithm of a float number, using the function in Python. Let us look into the code of the same.
Code:
Output:
Explanation: In the above example of log2 in python, we have computed the logarithm base 2 of a decimal number using the function in Python. As expected, it returned us the appropriate result for the same. Hence, positive or decimal value numbers are valid input for the function in Python.
Please note that it returns a float value of <class 'float'> as output (and not an integer).
Example 5 - Calculating Logarithm of Numbers from List and Tuple
In this example of log2 in python, we will be calculating the logarithm of numbers from lists and tuples, using the function in Python. Let us look into the code of the same.
Code:
Output:
Explanation: In the above example, we calculated the logarithm base 2 for the values from the elements in the lists, tuples, set, and dictionaries. Likewise can be done for the elements in various forms in python. So, this shows that we can use the function in Python to calculate the values of the elements in different data structures in Python as well.
Related Functions in Python
Now that you have got a clear and crisp idea about log2 function in Python, I encourage you to go ahead and pick any of the below scaler articles to further enhance your knowledge in Python –
Conclusion
In this article, we learned about the "log2 function in Python". Let us now jolt down the points to summarise what we learned till now --
- The logarithm of a given integer x is the exponent to which any defined number of base be, must be **raised in order** to obtain that number x`.
- The Python math.log2() function is an in-built function of the math module that is used to obtain the base-2 logarithm of a given number.
- The function in Python takes one number as input and returns the logarithm base 2 of the number as output.
- If we pass a positive number as an argument to the function in Python, then the function returns the correct output.
- If we pass any negative value as an argument to the function in Python, then a ValueError is raised.
- If a non-numeric number is passed as an argument to the function in Python, then the TypeError is raised.