Python中的角度转换功能

Python Angular转换函数/方法

在python编程语言中,数学模块中定义了一些内置函数–它们可用于角度转换,转换角度值,有两个角度转换函数

  1. math.degrees()
    用于将角度值从弧度转换为度。

  2. math.radians()
    用于将角度值从度转换为弧度。

它的语法 math.degrees() 和 math.radians() 职能:

    math.degrees(x)
    math.radians(x)

示例

    Input:
    x = 10.25 # 弧度值

    # converting & printing in degrees
    x = math.degrees(x)
    print(x)

    # again, converting & printing in radians
    x = math.radians(x)
    print(x)

    Output:
    587.2817400090938
    10.25

Python代码演示角度转换函数的示例

# Python代码演示示例 
# math.degrees() and math.radians() methods

# 导入数学模块
import math 

# 弧度角x
x = 10.25
print("x in radians: ", x)

# 转换为度
x = math.degrees(x)print("x in degrees: ", x)

# 现在再次转换为弧度
x = math.radians(x)print("x in radians: ", x)

输出结果

x in radians:  10.25
x in degrees:  587.2817400090938
x in radians:  10.25

注意:如果我们向这些函数提供数字类型值以外的任何东西,它们将返回TypeError

# Python代码演示示例 
# math.degrees() with exception

# 导入数学模块
import math 

# 字符串中的角度x(无效)
x = "10.25"

# 转换为度
x = math.degrees(x)print("x in degrees: ", x)

输出结果

Traceback (most recent call last):
  File "/home/main.py", line 12, in <module>
    x = math.degrees(x)TypeError: a float is required