简体   繁体   English

在 python 2.7 中使用 PI

[英]Using PI in python 2.7

I am trying to access the value of pi in Python 2.7, but it seems as if Python doesn't recognize math.pi.我试图在 Python 2.7 中访问 pi 的值,但似乎 Python 无法识别 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".我正在使用 IDLE,当我尝试打印 math.pi 的值时,它说“数学未定义”或“数学未定义”。 I can't upgrade to the next version without risk, so is there a way to access pi in Python 2.7?我不能毫无风险地升级到下一个版本,那么有没有办法在 Python 2.7 中访问 pi? Alternatively, is there another way to convert degrees to radians in Python 2.7 without dealing with Pi?或者,是否有另一种方法可以在不处理 Pi 的情况下在 Python 2.7 中将度数转换为弧度?

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 modules and 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

To have access to stuff provided by math module, like pi .访问math模块提供的东西,比如pi You need to import the module first:您需要先导入模块:

import math
print (math.pi)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM