Chapter 1 Pythonの紹介¶
2 + 2
100 - 75
7 * 9
14 // 2
15 // 2
15 % 2
12 * 6 + 2
2 + 12 * 6
(2 + 12) * 6
12 * (6 + 2)
2 ** 3
2.0 + 2.0
15.3 / 2.5
6.0 * 1.5
2.5 ** 25.0
2.0 ** 500.0
1.33e+5 + 1.0
2 ** 500
100000000 * 500 # 100 million * 500
100_000_000 * 500 # can be a separator for large numbers
100 * 3.4 # mix of integers and floats -> float
100 + 3.4
100 ** 3.4
1000 / 10.2 # division is a special case
1000 / 10
1000 // 10.2
1000.1 // 10.2
float(5)
int(3.99999)
round(3.99999)
pi = 3.14159
radius = 8.0
height = 16
baseArea = pi * radius ** 2
baseArea
cylinderVolume = baseArea * height
cylinderVolume
a = 10
b = 20
a = b
a
b
import turtle
turtle
gertrude = turtle.Turtle()
gertrude
gertrude.forward(100)
gertrude.right(90)
gertrude.position()
gertrude.heading()
def drawSquare(myTurtle, sideLength):
myTurtle.forward(sideLength) # side 1
myTurtle.right(90)
myTurtle.forward(sideLength) # side 2
myTurtle.right(90)
myTurtle.forward(sideLength) # side 3
myTurtle.right(90)
myTurtle.forward(sideLength) # side 4
myTurtle.right(90)
from ds import *
import turtle
t = turtle.Turtle()
drawSquare(t, 150)
# turtle.mainloop()
def drawSquare(myTurtle, sideLength):
for i in range(4):
myTurtle.forward(sideLength)
myTurtle.right(90)
def drawSpiral(myTurtle, maxSide):
for sideLength in range(1, maxSide + 1, 5):
myTurtle.forward(sideLength)
myTurtle.right(90)
def drawTriangle(myTurtle, sideLength):
for i in range(3):
myTurtle.forward(sideLength)
myTurtle.right(120)
def drawPolygon(myTurtle, sideLength, numSides):
turnAngle = 360 / numSides
for i in range(numSides):
myTurtle.forward(sideLength)
myTurtle.right(turnAngle)
from dp import *
import turtle
t = turtle.Turtle()
t.up()
t.backward(200)
t.left(90)
t.down()
drawPolygon(t, 100, 4)
drawPolygon(t, 100, 8)
drawPolygon(t, 50, 20)
drawPolygon(t, 20, 20)
def drawCircle(myTurtle, radius):
circumference = 2 * 3.1415 * radius
sideLength = circumference / 360
drawPolygon(myTurtle, sideLength, 360)
from dp import *
import turtle
t = turtle.Turtle()
turtle.speed(0)
t.up()
t.backward(200)
t.left(90)
t.down()
drawCircle(t, 20)
drawCircle(t, 200)
turtle.mainloop()