{"id":25353,"date":"2021-11-10T09:38:07","date_gmt":"2021-11-10T04:08:07","guid":{"rendered":"https:\/\/python-programs.com\/?p=25353"},"modified":"2021-11-10T09:38:07","modified_gmt":"2021-11-10T04:08:07","slug":"python-math-log2-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-math-log2-method-with-examples\/","title":{"rendered":"Python math.log2() Method with Examples"},"content":{"rendered":"
math.log2() Method in Python:<\/strong><\/p>\n The math.log2() method returns a number’s base-2 logarithm.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n x:<\/strong> This is Required. Specifies the value for which the logarithm should be calculated.<\/p>\n Return Value:<\/strong><\/p>\n Returns a float value that represents a number’s base-2 logarithm.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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: …<\/p>\nmath.log2(x)<\/pre>\n
\n
Given Number = 4\r\nGiven List = [2, 3.5, 4, 5.62, 1]<\/pre>\n
The base-2 logarithm of a given number { 4 } = 2.0\r\nThe base-2 logarithm of a list element gvn_lst[1] = 1.8073549220576042<\/pre>\n
Given Number = 5.56\r\nGiven List = [4, 5, 2, 1, 6]<\/pre>\n
The base-2 logarithm of a given number { 5.56 } = 2.475084882948783\r\nThe base-2 logarithm of a list element gvn_lst[2] = 1.0<\/pre>\n
math.log2() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [2, 3.5, 4, 5.62, 1]\r\n# Give the number as static input and store it in another variable.\r\ngvn_numb = 4\r\n# Apply math.log2() method to the given number to get base-2 logarithm of a\r\n# given number.\r\n# Store it in another variable.\r\nrslt1 = math.log2(gvn_numb)\r\n# Print the above result.\r\nprint(\"The base-2 logarithm of a given number {\", gvn_numb, \"} =\", rslt1)\r\n# Apply math.log2() method to the given list to get base-2 logarithm of a list\r\n# element.\r\n# Store it in another variable.\r\nrslt2 = math.log2(gvn_lst[1])\r\n# Print the base-2 logarithm of a given listelement.\r\nprint(\"The base-2 logarithm of a list element gvn_lst[1] =\", rslt2)\r\n<\/pre>\n
The base-2 logarithm of a given number { 4 } = 2.0\r\nThe base-2 logarithm of a list element gvn_lst[1] = 1.8073549220576042<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the number as user input using the float(input()) function and \r\n# store it in another variable.\r\ngvn_numb = float(input(\"Enter some random number = \"))\r\n# Apply math.log2() method to the given number to get base-2 logarithm of a\r\n# given number.\r\n# Store it in another variable.\r\nrslt1 = math.log2(gvn_numb)\r\n# Print the above result.\r\nprint(\"The base-2 logarithm of a given number {\", gvn_numb, \"} =\", rslt1)\r\n# Apply math.log2() method to the given list to get base-2 logarithm of a list\r\n# element.\r\n# Store it in another variable.\r\nrslt2 = math.log2(gvn_lst[2])\r\n# Print the base-2 logarithm of a given list element.\r\nprint(\"The base-2 logarithm of a list element gvn_lst[2] =\", rslt2)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 4 5 2 1 6\r\nEnter some random number = 5.56\r\nThe base-2 logarithm of a given number { 5.56 } = 2.475084882948783\r\nThe base-2 logarithm of a list element gvn_lst[2] = 1.0<\/pre>\n","protected":false},"excerpt":{"rendered":"