{"id":24406,"date":"2021-10-14T09:46:12","date_gmt":"2021-10-14T04:16:12","guid":{"rendered":"https:\/\/python-programs.com\/?p=24406"},"modified":"2021-11-05T21:07:39","modified_gmt":"2021-11-05T15:37:39","slug":"python-program-for-tanh-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-tanh-function\/","title":{"rendered":"Python Program for tanh() Function"},"content":{"rendered":"
In the previous article, we have discussed Python Program for sinh() Function<\/a> The math.tanh() method returns a number’s hyperbolic tangent.<\/p>\n Syntax:<\/strong><\/p>\n Parameters:<\/strong><\/p>\n x:<\/strong> This is Required. It is a number for which the hyperbolic tangent must be calculated. If the value is not a number, a TypeError is returned.<\/p>\n Return Value:<\/strong><\/p>\n Returns a float value representing a number’s hyperbolic tangent.<\/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 sinh() Function tanh() Function in Python: The math.tanh() method returns a number’s hyperbolic tangent. Syntax: math.tanh(x) Parameters: x: This is Required. It is a number for which the hyperbolic tangent must be calculated. If the value is not a number, a TypeError is returned. Return …<\/p>\n
\ntanh() Function in Python:<\/strong><\/p>\nmath.tanh(x)<\/pre>\n
\n
Given first number = 7\r\nGiven second number = 5.5<\/pre>\n
The result after applying tanh() function on above given first number 7 = 0.9999983369439447\r\nThe result after applying tanh() function on above given second number 5.5 = 0.9999665971563038<\/pre>\n
Given first number = 2\r\nGiven second number = 1<\/pre>\n
The result after applying tanh() function on above given first number 2 = 0.9640275800758169\r\nThe result after applying tanh() function on above given second number 1 = 0.7615941559557649<\/pre>\n
Program for cosh() 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 = [-1, 1, 3, 0.8]\r\n# Give the first number as static input and store it in another variable.\r\ngvn_numb1 = 7\r\n# Apply math.tanh() function to the given first number to get the hyperbolic\r\n# tangent value of a given number.\r\n# Store it in another variable.\r\nfnl_rslt = math.tanh(gvn_numb1)\r\n# Print the above result\r\nprint(\"The result after applying tanh() 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 = 5.5\r\nprint(\"The result after applying tanh() function on above given second number\",\r\n gvn_numb2, \" = \", math.tanh(gvn_numb2))\r\n# Apply math.tanh() function to the given list element and print it.\r\nprint(\r\n \"The result after applying tanh() function on given list element gvnlst[1] = \", math.tanh(gvn_lst[1]))\r\n<\/pre>\n
The result after applying tanh() function on above given first number 7 = 0.9999983369439447\r\nThe result after applying tanh() function on above given second number 5.5 = 0.9999665971563038\r\nThe result after applying tanh() function on given list element gvnlst[1] = 0.7615941559557649<\/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.tanh() function to the given first number to get the hyperbolic\r\n# tangent value of a given number.\r\n# Store it in another variable.\r\nfnl_rslt = math.tanh(gvn_numb1)\r\n# Print the above result\r\nprint(\"The result after applying tanh() 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 tanh() function on above given second number\",\r\n gvn_numb2, \" = \", math.tanh(gvn_numb2))\r\n# Apply math.tanh() function to the given list element and print it.\r\nprint(\r\n \"The result after applying tanh() function on given list element gvnlst[2] = \", math.tanh(gvn_lst[2]))\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 2 3 10 4 1\r\nEnter some random number = 2\r\nThe result after applying tanh() function on above given first number 2.0 = 0.9640275800758169\r\nEnter some random number = 1\r\nThe result after applying tanh() function on above given second number 1.0 = 0.7615941559557649\r\nThe result after applying tanh() function on given list element gvnlst[2] = 0.9999999958776927<\/pre>\n
\n