Python math.log2() Method with Examples

math.log2() Method in Python:

The math.log2() method returns a number’s base-2 logarithm.

Syntax:

math.log2(x)

Parameters

x: This is Required. Specifies the value for which the logarithm should be calculated.

  • If the value is 0 or a negative number, a ValueError is returned.
  • If the value is not a number, a TypeError is returned.

Return Value:

Returns a float value that represents a number’s base-2 logarithm.

Examples:

Example1:

Input:

Given Number = 4
Given List = [2, 3.5, 4, 5.62, 1]

Output:

The base-2 logarithm of a given number { 4 } = 2.0
The base-2 logarithm of a list element gvn_lst[1] = 1.8073549220576042

Example2:

Input:

Given Number = 5.56
Given List = [4, 5, 2, 1, 6]

Output:

The base-2 logarithm of a given number { 5.56 } = 2.475084882948783
The base-2 logarithm of a list element gvn_lst[2] = 1.0

math.log2() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import math module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Give the number as static input and store it in another variable.
  • Apply math.log2() method to the given number to get base-2 logarithm of a given number.
  • Store it in another variable.
  • Print the above result.
  • Apply math.log2() method to the given list to get base-2 logarithm of a list element.
  • Store it in another variable.
  • Print the base-2 logarithm of a given list element.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the list as static input and store it in a variable.
gvn_lst = [2, 3.5, 4, 5.62, 1]
# Give the number as static input and store it in another variable.
gvn_numb = 4
# Apply math.log2() method to the given number to get base-2 logarithm of a
# given number.
# Store it in another variable.
rslt1 = math.log2(gvn_numb)
# Print the above result.
print("The base-2 logarithm of a given number {", gvn_numb, "} =", rslt1)
# Apply math.log2() method to the given list to get base-2 logarithm of a list
# element.
# Store it in another variable.
rslt2 = math.log2(gvn_lst[1])
# Print the base-2 logarithm of a given listelement.
print("The base-2 logarithm of a list element gvn_lst[1] =", rslt2)

Output:

The base-2 logarithm of a given number { 4 } = 2.0
The base-2 logarithm of a list element gvn_lst[1] = 1.8073549220576042

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import math module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the number as user input using the float(input()) function and store it in another variable.
  • Apply math.log2() method to the given number to get base-2 logarithm of a given number.
  • Store it in another variable.
  • Print the above result.
  • Apply math.log2() method to the given list to get base-2 logarithm of a list element.
  • Store it in another variable.
  • Print the base-2 logarithm of a given list element.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the number as user input using the float(input()) function and 
# store it in another variable.
gvn_numb = float(input("Enter some random number = "))
# Apply math.log2() method to the given number to get base-2 logarithm of a
# given number.
# Store it in another variable.
rslt1 = math.log2(gvn_numb)
# Print the above result.
print("The base-2 logarithm of a given number {", gvn_numb, "} =", rslt1)
# Apply math.log2() method to the given list to get base-2 logarithm of a list
# element.
# Store it in another variable.
rslt2 = math.log2(gvn_lst[2])
# Print the base-2 logarithm of a given list element.
print("The base-2 logarithm of a list element gvn_lst[2] =", rslt2)

Output:

Enter some random List Elements separated by spaces = 4 5 2 1 6
Enter some random number = 5.56
The base-2 logarithm of a given number { 5.56 } = 2.475084882948783
The base-2 logarithm of a list element gvn_lst[2] = 1.0