Python Program for tanh() Function

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 Value:

Returns a float value representing a number’s hyperbolic tangent.

  • The tanh() function returns the hyperbolic Tangent value if the number argument is positive or negative.
  • The tanh()  function returns TypeError if it is not a number.

Examples:

Example1:

Input:

Given first number = 7
Given second number = 5.5

Output:

The result after applying tanh() function on above given first number 7  =  0.9999983369439447
The result after applying tanh() function on above given second number 5.5  =  0.9999665971563038

Example2:

Input:

Given first number = 2
Given second number = 1

Output:

The result after applying tanh() function on above given first number 2  =  0.9640275800758169
The result after applying tanh() function on above given second number 1  =  0.7615941559557649

Program for cosh() Function 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 first number as static input and store it in another variable.
  • Apply math.tanh() function to the given first number to get the hyperbolic tangent value of a given number.
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.tanh() function to the given list element and print it.
  • The Exit of 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 = [-1, 1, 3, 0.8]
# Give the first number as static input and store it in another variable.
gvn_numb1 = 7
# Apply math.tanh() function to the given first number to get the hyperbolic
# tangent value of a given number.
# Store it in another variable.
fnl_rslt = math.tanh(gvn_numb1)
# Print the above result
print("The result after applying tanh() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = 5.5
print("The result after applying tanh() function on above given second number",
      gvn_numb2, " = ", math.tanh(gvn_numb2))
# Apply math.tanh() function to the given list element and print it.
print(
    "The result after applying tanh() function on given list element gvnlst[1] = ", math.tanh(gvn_lst[1]))

Output:

The result after applying tanh() function on above given first number 7  =  0.9999983369439447
The result after applying tanh() function on above given second number 5.5  =  0.9999665971563038
The result after applying tanh() function on given list element gvnlst[1] =  0.7615941559557649

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 first number as user input using the float(input()) function and store it in a variable.
  • Apply math.tanh() function to the given first number to get the hyperbolic tangent value of a given number.
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.tanh() function to the given list element and print it.
  • The Exit of 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(float, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give the first number as user input using the float(input()) function
# and store it in a variable.
gvn_numb1 = float(input("Enter some random number = "))
# Apply math.tanh() function to the given first number to get the hyperbolic
# tangent value of a given number.
# Store it in another variable.
fnl_rslt = math.tanh(gvn_numb1)
# Print the above result
print("The result after applying tanh() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = float(input("Enter some random number = "))
print("The result after applying tanh() function on above given second number",
      gvn_numb2, " = ", math.tanh(gvn_numb2))
# Apply math.tanh() function to the given list element and print it.
print(
    "The result after applying tanh() function on given list element gvnlst[2] = ", math.tanh(gvn_lst[2]))

Output:

Enter some random List Elements separated by spaces = 2 3 10 4 1
Enter some random number = 2
The result after applying tanh() function on above given first number 2.0 = 0.9640275800758169
Enter some random number = 1
The result after applying tanh() function on above given second number 1.0 = 0.7615941559557649
The result after applying tanh() function on given list element gvnlst[2] = 0.9999999958776927

Know all about Mathematical Functions in Python by going through our Python Mathematical Methods Examples well explained step by step.