Python math.tanh() Method

Python math.tanh() method “returns the hyperbolic tangent value of a number.”




var: It takes values of numeric datatype and throws a TypeError if the argument 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.

Example 1: How to Use math.tanh() Method

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))


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

Example 2: TypeError: must be a real number, not str

import math

x = 'h'


TypeError: must be a real number, not str

Example 3: Plotting the math.tanh() Method

import matplotlib.pyplot as plt
import math

# Create a list of x values from -2 to 2
x = [i * 0.01 for i in range(-200, 201)]

# Calculate the corresponding y values
y = [math.tanh(i) for i in x]

# Create a new figure

# Plot x against y
plt.plot(x, y)

# Set the title
plt.title('Plot of tanh(x)')

# Set the x and y axis labels

# Display the plot


Plotting the math.tanh() Method

That’s it.

Leave a Comment

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