AppDividend
Latest Code Tutorials

Python tan: How to Use Math.tan() in Python Example

0

In Python, some of the most essential mathematical methods are defined in the math module. These include trigonometric functions, representation functions, logarithmic functions, angle conversion functions, etc. Python math.tan() function returns the tangent of the value passed as an argument.

Python tan()

Python tan() is an inbuilt method defined under the math module, which is used to find the tan value of the parameter passed in radians. For example, if x is passed as a parameter in tan function (tan(x)), it returns the tan value of x in radians.

We can use a math module by importing it. The syntax for it would be import math; after importing, we use to 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

See also

Python sin()

Python hypot()

Python atan()

Python atan2()

Python acos()

Leave A Reply

Your email address will not be published.

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