Chapter 1 Pythonの紹介

session01-01.py
2 + 2
100 - 75
7 * 9
14 // 2
15 // 2
15 % 2
session01-02.py
12 * 6 + 2
2 + 12 * 6
(2 + 12) * 6
12 * (6 + 2)
2 ** 3
session01-03.py
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
session01-04.py
2 ** 500
100000000 * 500  # 100 million * 500
100_000_000 * 500  # can be a separator for large numbers
session01-05.py
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
session01-06.py
float(5)
int(3.99999)
round(3.99999)
session01-07.py
pi = 3.14159
radius = 8.0
height = 16
baseArea = pi * radius ** 2
baseArea
cylinderVolume = baseArea * height
cylinderVolume
session01-08.py
a = 10
b = 20
a = b
a
b
session01-09.py
import turtle
turtle
gertrude = turtle.Turtle()
gertrude
gertrude.forward(100)
gertrude.right(90)
gertrude.position()
gertrude.heading()
listing01-1.01.py
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)
session01-10.py
from ds import *
import turtle
t = turtle.Turtle()
drawSquare(t, 150)
# turtle.mainloop()
listing01-1.02.py
def drawSquare(myTurtle, sideLength):
    for i in range(4):
        myTurtle.forward(sideLength)
        myTurtle.right(90)
listing01-1.03.py
def drawSpiral(myTurtle, maxSide):
    for sideLength in range(1, maxSide + 1, 5):
        myTurtle.forward(sideLength)
        myTurtle.right(90)

        
listing01-1.04.py
def drawTriangle(myTurtle, sideLength):
    for i in range(3):
        myTurtle.forward(sideLength)
        myTurtle.right(120)
listing01-1.05.py
def drawPolygon(myTurtle, sideLength, numSides):
    turnAngle = 360 / numSides
    for i in range(numSides):
        myTurtle.forward(sideLength)
        myTurtle.right(turnAngle)
session01-11.py
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)
listing01-1.06.py
def drawCircle(myTurtle, radius):
    circumference = 2 * 3.1415 * radius
    sideLength = circumference / 360
    drawPolygon(myTurtle, sideLength, 360)
    
session01-12.py
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()