AppDividend
Latest Code Tutorials

Python cosh() Function | Math.cosh() in Python Example

0

Python cosh() is an inbuilt method that is defined under the math module, which is used to find a hyperbolic cosine of the given parameter in radians. For instance, if x is passed as a parameter in the cosh function (cosh(x)), it returns the hyperbolic cosine value. We can use the math module by importing it. 

Python cosh()

Python cosh function is one of the Python Math function, which calculates the Trigonometric Hyperbolic Cosine for the specified expression. Another important point to note is that the math cosh() function only takes parameter values of number type if any other type is passed; it returns TypeError.

Syntax

math.cosh(var)

Here var is the variable of which hyperbolic cosine we have to find.

a

It takes one parameter var, which takes values of numeric datatype and throws TypeError if an argument is of any other data type is passed.

Return Value

Example 1: Write a program to show the working of the cosh() method in Python.

import math
a1 = 2.4
b1 = 1
c1 = 7
d1 = 5.3

print("Value for parameter ", a1, " is ", math.cosh(a1))
print("Value for parameter ", b1, " is ", math.cosh(b1))
print("Value for parameter ", c1, " is ", math.cosh(c1))
print("Value for parameter ", d1, " is ", math.cosh(d1))

Output

Value for parameter  2.4  is  5.556947166965506
Value for parameter  1  is  1.5430806348152437
Value for parameter  7  is  548.3170351552121
Value for parameter  5.3  is  100.17090078434929

In this example, we have seen that by passing a vaild parameter, which is different for different examples, we get the desired cosh() method solution, which is the hyperbolic cosine value of the parameter.

Example 2: Write a program to pass a value out of range from the cosh() function and display the output.

import math

q = "H"
print(math.cosh(q))

Output

TypeError: must be real number, not str

In this example, we’ve seen that by passing the parameter which is not of number type, the function throws an error.

Python cosh() with Python list and tuple

See the following code.

# app.py

import math

Tup = (11, 21, 30, -4, 5)
Lis = [-11, 21, -3.5, -4, 5]

print('Python Hyperbolic Cosine value of Positive Number = %.2f' % math.cosh(11))
print('Python Hyperbolic Cosine value of Negative Number = %.2f' % math.cosh(-21))

print('Python Hyperbolic Cosine value of Tuple Item = %.2f' %
      math.cosh(Tup[3]))
print('Python Hyperbolic Cosine value of List Item = %.2f' % math.cosh(Lis[2]))

print('Python Hyperbolic Cosine value of Multiple Numbers = %.2f' %
      math.cosh(2 + 9 - 7))

print('Python Hyperbolic Cosine value of String Value = ', math.cosh('AppDividend'))

Output

First, we used the cosh Function directly on both the Positive integer and negative integer.

Next, we used the cosh Function on Python Tuple and Python List items. If you observe the above output, the cosh() function is working correctly on them.

Conclusion

Python cosh() calculates the Trigonometric Hyperbolic Cosine for the specified expression.

See also

Python degrees()

Python tan()

Python sin()

Python acosh()

Python asinh()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.