This article is more than ten years old and potentially contains out-dated information.
작성한지 10년 이상 지난 게시물입니다. 최신의 정보와 맞지 않는 내용을 포함할 수도 있습니다.

I barely started using Python today. I made a piece of code that calculates the value of PI for practice. You’re welcome to criticize, but politely, my first ever Python code.

Source Code

I apology if this makes you unhappy because it doesn’t look like python code. Trust me. It will be a lot better if I keep using Python.


import time, threading

class PrintValue(threading.Thread):
    def run(self):
        while 1:
            print x

i = 1
x = 0.0

printvalue = PrintValue()

while 1:
        x += 4.0/(i*2-1)
        x -= 4.0/(i*2-1)
    i = i + 1

As you can see, this code will never stop. It will print out the value of PI every second. If you want to stop, give it a substitute character (Ctrl + Z).

Also, I believe this is not thread-safe. However, it probably won’t cause any problem because PrintValue does not change the value of x. The only possible problem here, as far as I can tell, is the inaccuracy of the value of x printed on the screen at the moment when the statement print x (under is executed. Well, the value of x is an approximation of the value of PI anyway ;-)



  • Fully utilizing two processor cores on my MacBook
  • Measurement of time to calculate up to a certain number of digits