在 python 2.7 中使用 PI

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18841703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:02:31  来源:igfitidea点击:

Using PI in python 2.7

pythonpython-2.7

提问by Jeremy Fisher

I am trying to access the value of pi in Python 2.7, but it seems as if Python doesn't recognize math.pi. I am using IDLE, and when I try to print the value of math.pi, it says that "Math is not defined" or "math is not defined". I can't upgrade to the next version without risk, so is there a way to access pi in Python 2.7? Alternatively, is there another way to convert degrees to radians in Python 2.7 without dealing with Pi?

我试图在 Python 2.7 中访问 pi 的值,但似乎 Python 无法识别 math.pi。我正在使用 IDLE,当我尝试打印 math.pi 的值时,它说“数学未定义”或“数学未定义”。我不能毫无风险地升级到下一个版本,那么有没有办法在 Python 2.7 中访问 pi?或者,是否有另一种方法可以在不处理 Pi 的情况下在 Python 2.7 中将度数转换为弧度?

采纳答案by Tim Pietzcker

Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> math.pi
3.141592653589793

Check out the Python tutorial on modulesand how to use them.

查看有关模块Python 教程以及如何使用它们。

As for the second part of your question, Python comes with batteries included, of course:

至于你问题的第二部分,Python 自带电池,当然:

>>> math.radians(90)
1.5707963267948966
>>> math.radians(180)
3.141592653589793

回答by katznboyz

To have access to stuff provided by mathmodule, like pi. You need to import the module first:

访问math模块提供的东西,比如pi. 您需要先导入模块:

import math
print (math.pi)