{"id":24250,"date":"2021-10-14T09:18:47","date_gmt":"2021-10-14T03:48:47","guid":{"rendered":"https:\/\/python-programs.com\/?p=24250"},"modified":"2021-11-05T21:02:29","modified_gmt":"2021-11-05T15:32:29","slug":"python-program-for-log-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-log-function\/","title":{"rendered":"Python Program for log() Function"},"content":{"rendered":"
In the previous article, we have discussed Python Program for expm1() Function<\/a> The math.log() method returns the natural logarithm of a number or the base logarithm of a number.<\/p>\n Syntax:<\/strong><\/p>\n Parameters:<\/strong><\/p>\n x: <\/strong>This is required. It is a number. It 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.<\/p>\n base:<\/strong> This is optional. The logarithmic base that should be used. ‘e’ is the default.<\/p>\n Return Value:<\/strong><\/p>\n Returns a float value corresponding to the natural logarithm of a number or the logarithm of a number to base.<\/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 Know all about Mathematical Functions in Python by going through our Python Mathematical Methods Examples<\/a> well explained step by step.<\/p>\n In the previous article, we have discussed Python Program for expm1() Function log() Function in Python: The math.log() method returns the natural logarithm of a number or the base logarithm of a number. Syntax: math.log(x,\u00a0base) Parameters: x: This is required. It is a number. It Specifies the value for which the logarithm should be calculated. …<\/p>\n
\nlog() Function in Python:<\/strong><\/p>\nmath.log(x,\u00a0base)<\/pre>\n
\n
Given first number = 1\r\nGiven second number = 6<\/pre>\n
The result after applying log() function on above given first number 1 = 0.0\r\nThe result after applying log() function on above given second number 6 = 1.791759469228055<\/pre>\n
Given first number = 7\r\nGiven second number = 9<\/pre>\n
The result after applying log() function on above given first number 7 = 1.9459101490553132\r\nThe result after applying log() function on above given second number 9 = 2.1972245773362196<\/pre>\n
Program for log() Function 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 = [10, 5, 20, 25, 12]\r\n# Give the first number as static input and store it in another variable.\r\ngvn_numb1 = 1\r\n# Apply math.log() function to the given first number to get the natural logarithm\r\n# or the base logarithm of a given number.\r\n# Store it in another variable.\r\nfnl_rslt = math.log(gvn_numb1)\r\nprint(\"The result after applying log() function on above given first number\",\r\n gvn_numb1, \" = \", fnl_rslt)\r\n# similarly do the same for the other number.\r\ngvn_numb2 = 6\r\nprint(\"The result after applying log() function on above given second number\",\r\n gvn_numb2, \" = \", math.log(gvn_numb2))\r\n# Apply math.log() function to the given list element and print it.\r\nprint(\r\n \"The result after applying log() function on given list element gvnlst[1] = \", math.log(gvn_lst[1]))\r\n<\/pre>\n
The result after applying log() function on above given first number 1 = 0.0\r\nThe result after applying log() function on above given second number 6 = 1.791759469228055\r\nThe result after applying log() function on given list element gvnlst[1] = 1.6094379124341003<\/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(float, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the first number as user input using the float(input()) function\r\n# and store it in a variable.\r\ngvn_numb1 = float(input(\"Enter some random number = \"))\r\n# Apply math.log() function to the given first number to get the natural logarithm\r\n# or the base logarithm of a given number.\r\n# Store it in another variable.\r\nfnl_rslt = math.log(gvn_numb1)\r\nprint(\"The result after applying log() function on above given first number\",\r\n gvn_numb1, \" = \", fnl_rslt)\r\n# similarly do the same for the other number.\r\ngvn_numb2 = float(input(\"Enter some random number = \"))\r\nprint(\"The result after applying log() function on above given second number\",\r\n gvn_numb2, \" = \", math.log(gvn_numb2))\r\n# Apply math.log() function to the given list element and print it.\r\nprint(\r\n \"The result after applying log() function on given list element gvnlst[2] = \", math.log(gvn_lst[2]))\r\n\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 4 5 3 1 2\r\nEnter some random number = 7\r\nThe result after applying log() function on above given first number 7.0 = 1.9459101490553132\r\nEnter some random number = 9\r\nThe result after applying log() function on above given second number 9.0 = 2.1972245773362196\r\nThe result after applying log() function on given list element gvnlst[2] = 1.0986122886681098<\/pre>\n
\n