Python 数学常数
在本文中,我们将介绍Python的数学常数以及如何使用它们。
一些定义的常数可以在各种数学运算中使用,这些数学常数返回与它们的标准定义值等效的值。
以下是Python编程语言中可用的math模块常量:
Python math.e常数
数学常数返回Euler’s数,即 2.71828182846 。
语法
math.e
返回值 - 它返回表示数学常数e的浮点值2.71828182846。
示例
步骤
下面是执行所需任务的算法/步骤-
- 使用import关键字导入math模块。
-
使用(.)操作符和math模块的e常量打印Euler值e。
示例
以下程序使用Python中的math模块返回math.e常数的值-
# importing math module
import math
# printing the Euler value e
print ("The Euler value e = ", math.e)
输出
在执行后,上述程序将生成以下输出−
The Euler value e = 2.718281828459045
Python math.pi常量
math.pi常量返回pi的值3.14159265359。它被描述为圆的周长与直径的比值。
语法
math.pi
返回值: 它返回浮点数值 3.14159265359 ,代表数学常量π。
示例
步骤
以下是执行所需任务的算法/步骤:
- 使用import关键字导入 math 模块。
-
使用math模块的pi常量和(.)运算符打印 pi 的值。
示例
以下程序使用Python中的math模块返回math.pi常量的值-
# importing math module
import math
# printing the pi value
print ("The pi value = ", math.pi)
输出
在执行程序时,上述程序将生成以下输出:
The pi value = 3.141592653589793
Python math.tau 常数
math.tau 常数返回的值是 6.283185307179586 ,它被描述为圆周长与半径之比。
语法
math.tau
返回值 − 返回浮点值6.283185307179586,表示数学常数 tau 。
示例
步骤
以下是执行所需任务的算法/步骤:
- 使用import关键字导入 math 模块。
-
使用math模块的 tau 常量和(.)操作符打印 tau 值。
示例
以下程序使用Python的math模块返回math.tau常量的值 –
# importing math module
import math
# printing the tau value
print ("The tau value = ", math.tau)
输出
执行上述程序后,将生成以下输出 –
The tau value = 6.283185307179586
Python math.inf 常量
math.inf 常量返回正无穷大的值。使用 -math.inf 表示负无穷大。
inf 常量与 float(“inf”) 相同。
语法
math.inf
返回值 : 它返回一个浮点数,表示 正无穷大 的值。
示例
步骤
下面是执行所需任务的算法/步骤:
- 使用import关键字导入 math 模块。
-
使用数学模块的inf常量和(.)运算符打印正无穷大的值。
-
使用数学模块的inf常量和(.)运算符打印负无穷大的值,并将 -(负号) 应用于它。
示例
以下程序使用Python中的数学模块返回math.inf常量的值 –
# importing math module
import math
# printing the value of positive infinity
print ("The value of positive infinity = ", math.inf)
# printing the value of negative infinity
print ("The value of negative infinity = ", -math.inf)
输出
执行以上程序后,将会产生以下输出:
The value of positive infinity = inf
The value of negative infinity = -inf
Python math.nan常数
浮点型 nan (Not a Number)值由math.nan常数返回。这不是一个有效的数字。float(“nan”)与nan常数相似。
语法
math.nan
返回值 :它返回浮点值, nan (不是一个数字)
示例
步骤
以下是执行所需任务的算法/步骤:
- 使用import关键字导入math模块。
-
使用math模块的nan常量和(.)运算符打印nan值。
示例
以下程序使用Python中的math模块返回math.nan常量的值-
# importing math module
import math
# printing the nan value
print ("The nan value = ", math.nan)
输出
在执行的过程中,上述程序将会生成以下输出:
The nan value = nan
结论
在本文中,我们学习了Python中的数学常量。我们还演示了如何在Python程序中使用这些常量,并提供了一些示例。