log2 Function in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

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 log2(x)log_2(x) 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 log2(x)log_2(x) 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 log2log_2 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 log2log_2 function in Python.

  1. If we pass a positive number as an argument to the log2log_2 function in Python, then the function returns the correct output.
  2. In case we pass a negative number as the argument, then the log2log_2 function raises a ValueError exception and returns the same.
  3. However, if we pass some non-numeric argument(not any number) then the log2log_2 function returns the TypeError exception.

Exceptions of the log2 in Python

There are basically 2 exceptions of the log2log_2 function in Python. We have already covered it earlier, but let us specifically look into them now --

  1. If we pass any negative value as an argument to the log2log_2 function in Python, then a ValueError exception is raised.
  2. If a non-numeric number is passed as an argument to the log2log_2 function in Python, then the TypeError exception is raised. Apart from that, the log2log_2 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 log2log_2 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 log2log_2 function in Python has the following uses:

  • The log2log_2 function is used for computing the logarithm of a number raised to the base 2.
  • The log2log_2 function is considered even more accurate than the log(x, 2) in-built function in Python.
  • The log2log_2 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 log2log_2 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 log2log_2 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 log2log_2 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 log2log_2 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 log2log_2 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 log2log_2 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 log2log_2 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 log2log_2 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 log2log_2 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 log2log_2 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 log2log_2 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 log2log_2 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 log2log_2 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 log2log_2 function in Python to calculate the values of the elements in different data structures in Python as well.

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 log2log_2 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 log2log_2 function in Python, then the function returns the correct output.
  • If we pass any negative value as an argument to the log2log_2 function in Python, then a ValueError is raised.
  • If a non-numeric number is passed as an argument to the log2log_2 function in Python, then the TypeError is raised.