Different numbers of digits in PI

I am new to Python and I have doubts about PI.

>>> import math
>>> p = math.pi
>>> print p
3.14159265359
>>> math.pi
3.141592653589793
  • Why are there two different numbers?
  • How can I get the Pi value to more than decimal places without using the Chudnovsky algorithm?
+4
source share
2 answers

Why do two have a different number of digits?

One of them is "calculated" with __str__, the other with __repr__:

>>> print repr(math.pi)
3.141592653589793
>>> print str(math.pi)
3.14159265359

printuses the return value of __str__objects to determine what needs to be printed. Just math.piuses __repr__.

How can I get the Pi value to more than decimal places without using the Chudnovsky algorithm?

You can show more numbers using format()so

>>> print "pi is {:.20f}".format(math.pi)
pi is 3.14159265358979311600

20 - .

+13

print . , :

print "%1.<number>f" % math.pi

:

print "%1.11f" % math.pi
+3

All Articles