《Python语言程序设计》第一章.练习与作业

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 224,383评论 6 522
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 96,028评论 3 402
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 171,486评论 0 366
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 60,786评论 1 300
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 69,793评论 6 399
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 53,297评论 1 314
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 41,685评论 3 428
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 40,655评论 0 279
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 47,192评论 1 324
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 39,223评论 3 345
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 41,343评论 1 354
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,966评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 42,656评论 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 33,130评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 34,257评论 1 275
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 49,869评论 3 381
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 46,407评论 2 365