Chapter 2 piの計算

session 02-01.py
22 / 7
355 / 113
import math
9801 / (2206 * math.sqrt(2))
session 02-02.py
help("math")
session 02-03.py
import math
numSides = 8
innerAngleB = 360.0 / numSides
halfAngleA = innerAngleB / 2
oneHalfSides = math.sin(math.radians(halfAngleA))
sideS = oneHalfSides * 2
polygonCircumference = numSides * sideS
pi = polygonCircumference / 2
pi
listing02-01.py
def functionName(param1, param2, ...):
    statement1
    statement2
    ...
    return expression  # exit and return expression's value
    # to the caller
listing02-02.py
import math


def archimedes(numSides):
    innerAngleB = 360.0 / numSides
    halfAngleA = innerAngleB / 2
    oneHalfSides = math.sin(math.radians(halfAngleA))
    sides = oneHalfSides * 2
    polygonCircumference = numSides * sides
    pi = polygonCircumference / 2
    return pi
session 02-04.py
import math

archimedes
archimedes(8)
archimedes(16)
archimedes(100)

for sides in range(8, 100, 8):
    print(sides, archimedes(sides))
session 02-05.py
acc = 0
for x in range(1, 6):
    acc = acc + x

acc
listing02-03.py
def leibinz(terms):
    acc = 0
    num = 4
    den = 1

    for aTerm in range(terms):
        nextTerm = num / den * (-1)**aTerm
        acc = acc + nextTerm  # add term to accumulator
        den = den + 2  # prepare for next fraction

    return acc
session 02-06.py
leibinz(100)
leibinz(1000)
listing02-04.py
def wallis(pairs):
    acc = 1
    num = 2
    for aPair in range(pairs):
        leftTerm = num / (num - 1)
        rightTerm = num / (num + 1)

        acc = acc * leftTerm * rightTerm

        num = num + 2

    pi = acc * 2
    return pi
session 02-07.py
wallis(100)
wallis(1000)
wallis(10000)
session 02-08.py
import random
random.random()
random.random()

for i in range(5):
    print(random.random())
session 02-09.py
dogWeight = 25  # assign 25 to dogWeight
dogWeight
dogWeight == 25  # is dogWeight equl to 25?
dogWeight != 25  # is dogWeight not equl to 25?
dogWeight < 25  # is dogweight less than 25?
dogWeight <= 25  # is dogWeight less than or equal to 25?
dogWeight > 25  # is dogWeight greater than 25?
dogWeight >= 25  # is dogWeight greater than or equal to 25?
dogWeight == 5 * 5  # is dogWeight equal to expression 5 * 5?
catWeight = 18
dogWeight == catWeight  # compare dogWeight to another variable
session 02-10.py
6 < 10 and 3 < 7
6 < 10 and 10 < 6
4 != 4 or 5 < 8
4 != 4 or 8 < 5
not 6 < 10
not 6 > 10
session 02-11.py
a = 5
b = 3
if a > b:
    c = 10  # executed only if a > b
else:
    c = 20  # excuted olnly if a <= b
c
session 02-12.py
a = 5
b = 3
c = 44
if a > b:  # condition is True
    c = 10  # this statement is executed
c
if b > a:  # condition is False
    c = 25  # this statement is not executed
c
listing02-05.py
import random
import math


def montePi(numDarts):

    inCircle = 0

    for i in range(numDarts):
        x = random.random()
        y = random.random()

        distance = math.sqrt(x**2 + y**2)

        if distance <= 1:
            inCircle = inCircle + 1

        pi = inCircle / numDarts * 4
        return pi
session 02-13.py
montePi(100)
montePi(1000)
montePi(10000)
listing02-06.py
import random
import math
import turtle

def showMontePi(numDarts):
    wn = turtle.Screen()
    drawingT = turtle.Turtle()

    wn.setworldcoordinates(-2, -2, 2, 2)
    turtle.tracer(False)  # no animation

    drawingT.up()
    drawingT.goto(0, -1)
    drawingT.down()
    drawingT.goto(0, 1)

    drawingT.up()
    drawingT.goto(-1, 0)
    drawingT.down()
    drawingT.goto(1, 0)

    inCircle = 0
    drawingT.up()

    for i in range(numDarts):
        x = random.random()
        y = random.random()

        distance = math.sqrt(x**2 + y**2)

        drawingT.goto(x, y)

        if distance <= 1:
            inCircle = inCircle + 1
            drawingT.color("blue")
        else:
            drawingT.color("red")

        drawingT.dot()
    pi = inCircle / numDarts * 4
    wn.exitonclick()

    return pi


showMontePi(1000)