Python Math tan() Method
Python Math tan() returns the tangent of the radian of x.
Python Math tan() Syntax
Here is the syntax of the tan() method:
import math
math.tan(x)
Note: tan() is not directly accessible, you need to import the math module and then call the method via the math static object.
Python Math tan() Parameters
- x : A value.
Python Math tan() Return Value
Returns the tangent of the x radian, with a value between -1 and 1.
Python Math tan() Example#1
The following shows an example of using the tan() method.
#!/usr/bin/python3
import math
print ("(tan(9) : ", math.tan(9))
print ("tan(-9) : ", math.tan(-9))
print ("tan(0) : ", math.tan(0))
print ("tan(math.pi) : ", math.tan(math.pi))
print ("tan(math.pi/4) : ", math.tan(math.pi/4))
print ("tan(math.pi/8) : ", math.tan(math.pi/8))
Output:
Python Math tan() Example#2
Graphical representation of the tan() function.
#!/usr/bin/python3
import math
import numpy as np
import matplotlib.pyplot as plt
in_array = np.linspace(0, np.pi, 10)
out_array = []
for i in range(len(in_array)):
out_array.append(math.tan(in_array[i]))
i += 1
print("in_array : ", in_array)
print("\nout_array : ", out_array)
# red for numpy.sin()
plt.plot(in_array, out_array, color = 'red', marker = "o")
plt.title("math.tan() in apidemos.com")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()
Output:
deepinout@ubuntu:~/apidemos.com$ python3 demos.py
in_array : [0. 0.34906585 0.6981317 1.04719755 1.3962634 1.74532925
2.0943951 2.44346095 2.7925268 3.14159265]
out_array : [0.0, 0.36397023426620234, 0.8390996311772799, 1.7320508075688767, 5.671281819617707, -5.671281819617711, -1.7320508075688783, -0.8390996311772804, -0.36397023426620256, -1.2246467991473532e-16]