Python math.tan() function | Get tangent value
Welcome to this tutorial. Here let’s know, how to implement the Python math.tan() function.
Using this function we can easily find the tangent value for a given angle without much effort.
Tan is the Tangent function which is a trigonometric function.
Its value can be obtained by dividing the length of the opposite side by the adjacent side in a right-angled triangle as we learned in mathematics.
In Python, we will be able to use the tan() function by importing the math module which contains the definition to obtain the tangent value for any given angle.
The math module contains definitions of various other functions that perform mathematical operations.
To learn more about the math module, Look into this article – Math module of Python.
We need to use an import statement to implement the modules in Python. Check the following article to know about the use of import statements:
In our code, we shall be using for loop to control the number of iterations.
Let’s understand the functioning of for loop :
For loop is a type of looping statement in programming which is used to perform iterations for a given range.
We shall take a simple code to learn its functionality.
seq=[1,2,3,4,5,6,7,8] for k in seq : print(k) print("end")
OUTPUT of above code : 1 2 3 4 5 6 7 8 end
In the above code, we can clearly see that the for loop prints the numbers in the list named as seq in a sequence that it contains.
Get Tangent value in Python using math.tan()
Now let’s see how can we do it.
import math for z in range(0,91,15): A=math.radians(z) B=math.tan(A) print("tan(",z,"):",B)
In the following code, we have even included radian function without which the tan() function would calculate the tangent value taking the angle in terms of a radian.
To get the angle in degrees we shall simply implement the math.radian() function.
This function converts the angle from radian to degree.
In our code, we have used variables z, A, B.
1. The variable z takes the angle value from the range which is in radians.
2. After getting the converted angle value from radian to degree.
3. Variable A stores the angle value in terms of degree.
We now send the data in the variable A to get its tangent value.
Finally, the calculated tangent value is stored in variable B.
By using the print statement we can print the obtained value for the given range of angle values.
RESULTANT OUTPUT: tan(0): 0.0 tan(15): 0.2679491924 tan(30): 0.5777350269 tan(45): 0.9999999999 tan(60): 1.7320508075 tan(75): 3.7320508075 tan(90): 1.6331239353