Python语言程序设计 封面.jpeg
编程题 1.1
print("Welcome to Python")
print("Welcome to Computer Science")
print("Programming is fun")
编程题 1.2
for i in range(5):
print('Welcome to Python')
编程题 1.3
# 本题不严谨,无法严肃对齐
print("F" * 7, " " * 3, "U", " " * 5, "U", " " * 3, "N" * 2, " " * 4, "N" * 2)
print( "F" * 2, " " * 4, " " * 3, "U", " " * 5, "U", " " * 3, "N" * 3, " " * 3, "N" * 2)
print( "F" * 7, " " * 3, "U", " " * 5, "U", " " * 3, "N" * 2, "", "N" * 1, " " * 1, "N" * 2)
print( "F" * 2, " " * 4, " " * 3, "", "U", " " * 3, "U", " " * 4, "N" * 2, " " * 2, "N", "N" * 2)
print( "F" * 2, " " * 4, " " * 5, "" * 3, "U" * 3, " " * 2, " " * 3, "N" * 2, " " * 3, "N" * 3)
编程题 1.4
i = 1
print('a', ' ' * 3, 'a^2', ' ' * 3, 'a^3')
print(i * 1, ' ' * 3, i * 1**2, ' ' * 5, i * 1**3)
print(i * 2, ' ' * 3, i * 2**2, ' ' * 5, i * 2**3)
print(i * 3, ' ' * 3, i * 3**2, ' ' * 5, i * 3**3)
print(i * 4, ' ' * 3, i * 4**2, ' ' * 4, i * 4**3)
编程题 1.5
# 1.5
a = 9.5 * 4.5
b = 2.5 * 3
c = 45.5 - 3.5
print((a - b) / c)
编程题 1.6
sum = 0
for i in range(1, 10): # range(start,stop,step=None)
sum = sum + i
print(sum)
编程题 1.7
max = 11 # max为分子项中的最大数
sum = 0
i = 0
for i in range(1 + max // 2):
sum = sum + 4 * 1 * ((-1)**i) / (1 + i * 2)
i = i + 1
print(sum)
编程题 1.8
r = 5.5
PI = 3.14 # 常量建议用大写表示
area = PI * (r**2) # 计算面积
perimeter = 2 * PI * r # 计算周长
print('area=', area)
print('perimeter=', perimeter)
编程题 1.9
width = 4.5
height = 7.9
area = width * height # 计算面积
perimeter = width * 2 + height * 2 # 计算周长
print(area)
print(perimeter)
编程题 1.10
total_mileage = 14 * 1.6
time = 45.3 / 60
speed = total_mileage / time
print('speed:', speed, 'miles/hour')
编程题 1.11
born_one_year = 365 * (24 * 3600 // 7) # 每年出生人口
dead_one_year = 365 * (24 * 3600 // 13) # 每年死亡人口
immigration = 365 * (24 * 3600 // 45) # 每年移民人口
Now_population = 3120324986 # 当前人口
i = 1
for i in range(5):
Total_population = Now_population + i * \
(born_one_year - dead_one_year + immigration) # Pycharm中自动换行,以 \ 表示
i = i + 1
print('接下来第{}年,人口是{}人。'.format(i, Total_population))
编程题 1.12
# 1.12,图 1-18a,绘制正方形
import turtle
turtle.setup(500, 500) # 设置窗口尺寸
turtle.penup()
turtle.goto(100, 100)
turtle.pendown()
for i in range(4):
turtle.right(90)
turtle.fd(200)
turtle.penup()
turtle.goto(-100, 0)
turtle.pendown()
turtle.fd(200)
turtle.penup()
turtle.goto(0, -100)
turtle.pendown()
turtle.right(-90)
turtle.fd(100)
turtle.penup()
turtle.goto(0, 100)
turtle.pendown()
turtle.right(180)
turtle.fd(100)
turtle.penup() # 签名,font 这里有坑,勿踩
turtle.goto(100, 200)
turtle.pendown()
turtle.write("Jackey1999", move=False,font=("Consolas", 16))
turtle.hideturtle()
turtle.done() # 运行结束不退出 turtle 绘图窗口
编程题 1.13
# 1.13,图 1-18b,绘制十字
import turtle
turtle.setup(500, 500) # 设置窗口尺寸
turtle.penup()
turtle.goto(-100, 0)
turtle.pendown()
turtle.fd(200)
turtle.penup()
turtle.goto(0, 100)
turtle.pendown()
turtle.right(90)
turtle.fd(200)
turtle.penup() # 签名,font 这里有坑,勿踩
turtle.goto(100, 200)
turtle.pendown()
turtle.write("Jackey1999", move=False,font=("Consolas", 16))
turtle.hideturtle()
turtle.done() # 运行结束不退出 turtle 绘图窗口
编程题 1.14
# 1.14,图 1-18c,绘制三角形 1
import turtle
turtle.setup(500, 500) # 设置窗口尺寸
turtle.right(60)
turtle.fd(200)
turtle.right(120)
turtle.fd(200)
turtle.right(120)
turtle.fd(200)
turtle.penup() # 签名,font 这里有坑,勿踩
turtle.goto(100, 200)
turtle.pendown()
turtle.write("Jackey1999", move=False,font=("Consolas", 16))
turtle.hideturtle()
turtle.done() # 运行结束不退出 turtle 绘图窗口
编程题 1.14-2
# 1.14,图 1-18c,绘制三角形 2
import turtle
turtle.setup(500, 500) # 设置窗口尺寸
turtle.circle(-100, 360, 3)
turtle.penup() # 签名,font 这里有坑,勿踩
turtle.goto(100, 200)
turtle.pendown()
turtle.write("Jackey1999", move=False,font=("Consolas", 16))
turtle.hideturtle()
turtle.done() # 运行结束不退出 turtle 绘图窗口
编程题 1.15
# 1.15,图 1-18d,绘制两个三角形
import turtle
turtle.setup(500, 500) # 设置窗口尺寸
turtle.right(60) # 绘制下方三角形
turtle.fd(200)
turtle.right(120)
turtle.fd(200)
turtle.right(120)
turtle.fd(200)
turtle.fd(200) # 绘制上方三角形
turtle.right(-120)
turtle.fd(200)
turtle.right(-120)
turtle.fd(200)
turtle.penup() # 签名,font 这里有坑,勿踩
turtle.goto(100, 200)
turtle.pendown()
turtle.write("Jackey1999", move=False,font=("Consolas", 16))
turtle.hideturtle()
turtle.done() # 运行结束不退出 turtle 绘图窗口
编程题 1.16
# 1.16,图 1-19a,绘制四个圆
import turtle
turtle.setup(500, 500) # 设置窗口尺寸
turtle.penup() # 第一象限,圈1
turtle.goto(30, 30)
turtle.pendown()
turtle.circle(30)
turtle.penup() # 第二象限,圈2
turtle.goto(-30, 30)
turtle.pendown()
turtle.circle(30)
turtle.penup() # 第三象限,圈3
turtle.goto(-30, -30)
turtle.pendown()
turtle.circle(30)
turtle.penup() # 第四象限,圈4
turtle.goto(30, -30)
turtle.pendown()
turtle.circle(30)
turtle.penup() # 签名,font 这里有坑,勿踩
turtle.goto(100, 200)
turtle.pendown()
turtle.write("Jackey1999", move=False,font=("Consolas", 16))
turtle.hideturtle()
turtle.done()
编程题 1.17
# 1.17,图 1-19b,绘制直线
import turtle
turtle.setup(500, 500) # 设置窗口尺寸
turtle.speed(1)
turtle.penup()
turtle.goto(-39, 49)
turtle.write(turtle.pos()) # 在窗口上标注当前坐标,精度暂未学
turtle.pendown()
turtle.goto(50, -50)
turtle.write(turtle.pos()) # 在窗口上标注当前坐标,
# 简写turtle.ht(),隐藏海龟。若显示,可使用turtle.showturtle(),简写turtle.st()
turtle.hideturtle()
turtle.penup() # 签名,font 这里有坑,勿踩
turtle.goto(100, 200)
turtle.pendown()
turtle.write("Jackey1999", move=False,font=("Consolas", 16))
turtle.hideturtle()
turtle.done()
编程题 1.18
# 1.18,图 1-19c,绘制五角星
import turtle
turtle.setup(500, 500) # 设置窗口尺寸
turtle.speed(1) # 调整绘图速度,便于观察+调教代码
turtle.penup() # 签名,font 这里有坑,勿踩
turtle.goto(100, 200)
turtle.pendown()
turtle.write("Jackey1999", move=False,font=("Consolas", 16))
turtle.penup() # 调整起点位置
turtle.goto(0, 100)
turtle.pendown()
turtle.right(72) # 调整海龟朝向
for i in range(5):
turtle.fd(200)
turtle.right(144)
turtle.setheading(72) # 调整海龟朝向
turtle.done()
编程题 1.19
# 1.19,图 1-20a,绘制多边形
import turtle
turtle.setup(500, 500) # 设置窗口尺寸
turtle.speed(1) # 调整绘图速度,便于调教代码
turtle.penup() # 设置起始坐标
turtle.right(60)
turtle.forward(150)
turtle.pendown()
turtle.setheading(30)
turtle.circle(150, 360, 6) # 使用内切多边形的画法
turtle.hideturtle() # 隐藏海龟
turtle.penup() # 签名,font 这里有坑,勿踩
turtle.goto(100, 200)
turtle.pendown()
turtle.write("Jackey1999", font=("Consolas", 16))
turtle.done()
编程题1.20、1.21、附加题,暂时未完成。
Jackey1999,2019-03-20