Python tan()函数| Python示例中的Math.tan()

Python tan()是在math模块下定义的内置方法,该方法用于查找以弧度为单位的参数的tan值。例如,如果在tan函数(tan(x))中将x作为参数传递,则它将以弧度返回x的tan值。我们可以通过导入使用数学模块。它的语法是导入数学。导入后,我们使用静态对象调用tan()函数。

Python tan()

在Python中,数学模块中定义了一些最基本的数学方法。这些函数包括三角函数,表示函数,对数函数,角度转换函数等。Python math.tan()函数返回作为参数传递的值的正切值。

句法

math.tan(var)

这里var是我们必须找到的tan的变量。

参量

tan()函数采用一个参数var,该参数采用数字数据类型的值,如果传递的参数是其他任何数据类型,则抛出TypeError。

返回值

它返回float数据类型中数字的tan值。

请参见以下示例。

import math

var = 0.36
print(math.tan(var))

Python中tan()方法的示例程序

示例1:编写一个程序来展示tan()方法在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))

输出量

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

在此示例中,我们已经看到,通过传递对于不同示例而言不同的有效参数,我们可以获得所需的tan()方法解决方案。

示例2:编写程序以将值传递到tan()函数范围之外,并显示输出。

import math

x = "H"
print(math.tan(x))

输出量

TypeError: must be real number, not str

在此示例中,我们传递的值不是实数,因此该程序引发了一个错误,指出该数字必须为实数。

请参阅第三个示例。

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

输出量

python3 app.py
The value of tangent of pi / 6 is :  0.5773502691896256

也可以看看

Python sin()

Python hypot()

Python atan()

Python atan2()

Python acos()

资讯来源:由0x资讯编译自APPDIVIDEND,版权归作者Ankit Lathiya所有,未经许可,不得转载
你可能还喜欢