Chapter 2 piの計算¶
22 / 7
355 / 113
import math
9801 / (2206 * math.sqrt(2))
help("math")
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
def functionName(param1, param2, ...):
statement1
statement2
...
return expression # exit and return expression's value
# to the caller
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
import math
archimedes
archimedes(8)
archimedes(16)
archimedes(100)
for sides in range(8, 100, 8):
print(sides, archimedes(sides))
acc = 0
for x in range(1, 6):
acc = acc + x
acc
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
leibinz(100)
leibinz(1000)
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
wallis(100)
wallis(1000)
wallis(10000)
import random
random.random()
random.random()
for i in range(5):
print(random.random())
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
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
a = 5
b = 3
if a > b:
c = 10 # executed only if a > b
else:
c = 20 # excuted olnly if a <= b
c
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
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
montePi(100)
montePi(1000)
montePi(10000)
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)