Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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?

like image 663
Jeremy Fisher Avatar asked Sep 17 '13 05:09

Jeremy Fisher


2 Answers

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.

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

>>> math.radians(90)
1.5707963267948966
>>> math.radians(180)
3.141592653589793
like image 189
Tim Pietzcker Avatar answered Oct 14 '22 17:10

Tim Pietzcker


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

import math
print (math.pi)
like image 3
katznboyz Avatar answered Oct 14 '22 18:10

katznboyz