Some of the most essential mathematical methods in Python are defined in the math module. These include trigonometric functions, representation functions, logarithmic functions, angle conversion functions, etc. Let’s discuss how to calculate tangent.
Python tan
Python tan() is a built-in math module function used to find the tan value of the parameter passed in radians. If x is passed as a parameter in the tan() function (tan(x)), it returns the tan value of x in radians.
We can use a math module by importing it. The syntax would be import math.
After importing, we call the tan() function using the static object.
Syntax
math.tan(var)
Here var is the variable of which tan we have to find.
Parameters
The tan() function 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
It returns the tan value of the number in the float datatype.
See the following example.
import math var = 0.36 print(math.tan(var))
Example programs on tan() method in Python
Example 1: Write a program to show the working of a tan() method in Python.
import math a1 = 0.36 b1 = 1 c1 = -1 d1 = -0.36 print("Value for parameter ", a1, " is ", math.tan(a1)) print("Value for parameter ", b1, " is ", math.tan(b1)) print("Value for parameter ", c1, " is ", math.tan(c1)) print("Value for parameter ", d1, " is ", math.tan(d1))
Output
Value for parameter 0.36 is 0.3764028516420269 Value for parameter 1 is 1.5574077246549023 Value for parameter -1 is -1.5574077246549023 Value for parameter -0.36 is -0.3764028516420269
In this example, we have seen that by passing a valid parameter which is different for different examples, we get the desired tan() method solution.
Example 2: Write a program to pass the value out of range from the tan() function and display the output.
import math x = "H" print(math.tan(x))
Output
TypeError: must be real number, not str
In this example, we passed a value that was not real, so this program threw an error stating that the number must be real.
See the third example.
# app.py import math a = math.pi / 6 # returning the value of tangent of pi / 6 print("The value of tangent of pi / 6 is : ", math.tan(a))
Output
python3 app.py The value of tangent of pi / 6 is : 0.5773502691896256