AppDividend
Latest Code Tutorials

# Python tanh() Function | Math.tanh() in Python Example

Python tanh() is an inbuilt method that is defined under the math module, which is used to find the hyperbolic tangent of the given parameter in radians. For instance, if x is passed as an argument in tanh function (tanh(x)), it returns the hyperbolic tangent value. We can use a math module by importing it. Another important point to note is that the math tanh() function only takes argument values of number type if any other type is passed; it returns TypeError.

## Python tanh()

Content Overview

Python tanh() method allows you to find a trigonometric Hyperbolic tangent for numeric values.

### Syntax

```math.tanh(var)
```

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

### Parameters

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

### Return Value

It returns the hyperbolic tangent value of the number in the float datatype.

If the number argument is a positive or negative number, the tanh function returns the hyperbolic Tangent value.

If the number argument is not a number, the tanh function returns TypeError.

See the following example.

```import math

var = 0.7
print(math.tanh(var))
```

### Example programs on tanh() method in Python

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

```import math

a1 = 0.3
b1 = 0.9
c1 = 0.7
d1 = 0.2

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

#### Output

```Value for parameter  0.3  is  0.2913126124515909
Value for parameter  0.9  is  0.7162978701990245
Value for parameter  0.7  is  0.6043677771171635
Value for parameter  0.2  is  0.197375320224904```

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

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

```import math

x = 'h'
print(math.tanh(x))
```

#### Output

```TypeError: must be a real number, not str
```

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

## Python tanh() with list and tuple

See the following code.

```# app.py

import math

Tup = (1, 2, 3, -4.5 , 5)
Lis = [-1, 2, 3.5, -4 , 5]

print('Python Hyperbolic Tangent value of Positive Number = %.2f' %math.tanh(1))
print('Python Hyperbolic Tangent value of Negative Number = %.2f' %math.tanh(-2))

print('Python Hyperbolic Tangent value of List Item = %.2f' %math.tanh(Lis[2]))

print('Python Hyperbolic Tangent of Multiple Numbers = %.2f' %math.tanh(2 + 9 - 7))

print('Python Hyperbolic Tangent value of String Value = ', math.tanh('Hello'))```

#### Output

```python3 app.py
Python Hyperbolic Tangent value of Positive Number = 0.76
Python Hyperbolic Tangent value of Negative Number = -0.96
Python Hyperbolic Tangent value of List Item = 1.00
Python Hyperbolic Tangent of Multiple Numbers = 1.00
Traceback (most recent call last):
File "app.py", line 13, in <module>
print('Python Hyperbolic Tangent value of String Value = ', math.tanh('Hello'))
TypeError: must be real number, not str```

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

Next, We used the tanh Function on Python Tuple and List items.

Then, We used the tanh Function directly on multiple values.

In the last statement, we tried the tanh Function on the String value, and it returns TypeError.