python程序设计第一章答案_Python语言程序程序设计-第一章习题解答

本文是Python语言程序设计书籍的学习笔记,通过完成1.1至1.21的习题,涵盖了基本的打印、算术运算、列表操作、函数应用以及图形绘制等内容,旨在通过实践提升Python编程能力。
摘要由CSDN通过智能技术生成

作为一名非计算机专业毕业的学生,一直想学Python用来解决工作中遇到的问题,但是对计算机一直摸不到好的门路,于是买到了Python语言程序程序设计这本书,用做习题的形式,积累自己的学习成果,相信不怕慢,只怕站。

# 1.1

print "Welcome to Python"

print "Welcome to Computer Science"

print "Program is fun"

# 1.2

print "Welcome to Python" * 5

# 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

value = [1,2,3,4]

print "a"," "*4,"a^2"," "*2,"a^3"," "*4

print "1"," "*4,"1"," "*5,"1"," "*4

print "2"," "*4,"4"," "*5,"8"," "*4

print "3"," "*4,"9"," "*5,"27"," "*4

print "4"," "*4,"16"," "*4,"64"," "*4

# 1.5

solve = (9.5*4.5 - 2.5*3)/(45.5-3.5)

print solve

# 1.6

sum = 0

for i in range(10):

sum+=i

print sum

# 1.7

from __future__ import division

pi = 0.0

for i in range(1,8):

pi += 4*(((-1)**(i+1))/(2*i-1))

print pi

from __future__ import division

pi = 0.0

for i in range(1,11):

pi += 4*(((-1)**(i+1))/(2*i-1))

print pi

# 1.7

import numpy as np

class sl():

"""docstring for sl"""

def __init__(self, radius):

super(sl, self).__init__()

self.radius = radius

def area():

s = np.pi*self.radius*self.radius

return s

def perimeter():

l = 2*np.pi*self.radius

return l

solve = sl()

radius = 5

print solve.area(radius)

def area(radius):

s = np.pi*radius*radius

return s

def perimeter(radius):

l = 2*np.pi*radius

return l

r = 5

# print "area is %s" % area(r),"perimeter is %s" perimeter(r)

print area(r),perimeter(r)

# 1.9

def s(width,height):

s = width*height

return s

def l(width,height):

l = 2*width+2*height

return l

print s(4.5,7.9),l(4.5,7.9)

# 1.10

def milePerHour(m,s,mile):

enmile = mile/1.6

hour = (m*60+s)/3600

enmile_h = enmile/hour

return enmile_h

print milePerHour(40,30,14)

# 1.11

def predictPop(year):

a0 = 3120324986

s = year*365*3600

predict_pop = a0+(s/7.0+s/45.0-s/13.0)

return predict_pop

print predictPop(1)

print predictPop(2)

print predictPop(3)

print predictPop(4)

print predictPop(5)

# 1.12

import turtle

turtle.forward(100)

turtle.right(90)

turtle.forward(200)

turtle.right(90)

turtle.forward(200)

turtle.right(90)

turtle.forward(100)

turtle.right(90)

turtle.forward(200)

turtle.right(90)

turtle.forward(100)

turtle.right(90)

turtle.forward(100)

turtle.right(90)

turtle.forward(100)

turtle.left(90)

turtle.forward(100)

turtle.right(90)

turtle.forward(100)

turtle.right(90)

turtle.forward(100)

turtle.right(90)

turtle.forward(100)

# 1.13

import turtle

turtle.right(0)

turtle.forward(200)

turtle.right(180)

turtle.forward(100)

turtle.right(90)

turtle.forward(100)

turtle.right(180)

turtle.forward(200)

# 1.14

import turtle

turtle.right(60)

turtle.forward(100)

turtle.right(120)

turtle.forward(100)

turtle.right(120)

turtle.forward(100)

# 1.15

import turtle

turtle.right(60)

turtle.forward(100)

turtle.right(120)

turtle.forward(100)

turtle.right(120)

turtle.forward(200)

turtle.left(120)

turtle.forward(100)

turtle.left(120)

turtle.forward(100)

# 1.16

import turtle

turtle.color('red')

turtle.penup()

turtle.goto(0,0)

turtle.pendown()

turtle.circle(50)

turtle.color('blue')

turtle.penup()

turtle.goto(100,0)

turtle.pendown()

turtle.circle(50)

turtle.color('orange')

turtle.penup()

turtle.goto(0,-100)

turtle.pendown()

turtle.circle(50)

turtle.color('yellow')

turtle.penup()

turtle.goto(100,-100)

turtle.pendown()

turtle.circle(50)

# 1.17

import turtle

import math

import numpy as np

from __future__ import division

turtle.penup()

turtle.goto(-39,48)

turtle.write("(-39,48)")

turtle.pendown()

turtle.goto(50,-50)

# turtle.pendown()

turtle.write("50,-50")

turtle.done()

# 1.18

import turtle

# import math

# import numpy as np

# from __future__ import division

turtle.forward(200)

turtle.right(144)

turtle.forward(200)

turtle.right(144)

turtle.forward(200)

turtle.right(144)

turtle.forward(200)

turtle.right(144)

turtle.forward(200)

# 1.19

import turtle

turtle.penup()

turtle.goto(40,-69.28)

turtle.pendown()

turtle.goto(-40,-69.28)

turtle.goto(-80,-9.8)

turtle.goto(-40,69)

turtle.goto(40,69)

turtle.goto(80,0)

turtle.goto(40,-69.28)

turtle.done()

# 1.20

import turtle

turtle.goto(100,0)

turtle.goto(150,50)

turtle.goto(50,50)

turtle.goto(0,0)

turtle.goto(0,100)

turtle.goto(100,100)

turtle.goto(100,0)

turtle.penup()

turtle.goto(0,100)

turtle.pendown()

turtle.goto(50,150)

turtle.goto(150,150)

turtle.goto(100,100)

turtle.penup()

turtle.goto(50,150)

turtle.pendown()

turtle.goto(50,50)

turtle.penup()

turtle.goto(150,150)

turtle.pendown()

turtle.goto(150,50)

turtle.done()

# 1.21

import turtle

turtle.color("blue")

turtle.penup()

turtle.goto(0,0)

turtle.pendown()

turtle.circle(100)

turtle.penup()

turtle.goto(0,0)

turtle.pendown()

turtle.write("6")

turtle.penup()

turtle.goto(0,183)

turtle.pendown()

turtle.write("12")

turtle.penup()

turtle.goto(-96,100)

turtle.pendown()

turtle.write("9")

turtle.penup()

turtle.goto(90,100)

turtle.pendown()

turtle.write("3")

turtle.penup()

turtle.goto(0,100)

turtle.pendown()

turtle.goto(0,180)

turtle.penup()

turtle.goto(0,100)

turtle.pendown()

turtle.goto(-90,100)

turtle.penup()

turtle.goto(0,100)

turtle.pendown()

turtle.goto(80,100)

随着国家大数据战略的深入实施,各行业智慧化建急需数据分析人才和智能应用人才。智慧化简单来说是一个以机器替换人力的过程,而机器的“灵魂”是程序Python已经成为公认的驱动大数据智能应用的主流编程语言Python程序设计的书籍已经琳琅满目,每一本书都凝聚了作者对Python的理解和对程序设计的认识,都是作者编程开发和教学经验的总结,都折射出作者的专业背景。由于大数据专业学生对程序设计的要求不是很高,但又需要具备一定的计算思维能力,熟悉用程序进行数据分析的一般流程,因此程序设计教材要言不甚深、文不甚俗,既要覆盖相关技术,又不能面面俱到,注重对问题的分析和解释,用程序表达算法。鉴于此,我们编写了本书。 本书每一章的标题都以Python开头,凸显Python在各个部分都有其独特的编程理念和方法。其他高级编程语言如C、C++和Java等相比,Python在数据的表示、处理和可视化方面都有绝对的优势。有编程基础的学习者在学习Python时最好能忘掉以往程序设计语言的语法,彻底转变观念,以全新的姿态融入到Python的编程特点和规律之中。如变量定义、数据类型、数据结构、控制结构、类和对象、文件访问、数据分析和可视化,每一部分都有其特别之处,都值得我们重新认识,重新使用,重新熟悉。每一章开始的思维导图都是对本章技术脉络的梳理,开门见山地给学习者展示本章的知识和技术体系,以便学习者在学习过程中始终能保持思路清晰和整体把握。每一章开头的本章导读都是编者多年来程序开发计教学经验的提炼升华,都是对程序设计的理解和感悟,值得学习者深入领会。每一章开头的本章要点都是要求学习者深入理解的重要知识和熟练掌握的关键技术。每一章的小结都是对本章要点的具体解释,供学习者复习查询。 本书为河北省高等教育教学改革研究实践项目“新工科背景下警务大数据应用专业人才培养模式教学实践研究”(编号:2018GJJG450)的阶段性成果。 下面是本书的体系结构图。 第1章Python编程初步。学习本章,要了解Python作为一种计算机程序设计脚本语言,结合了解释性、编译性和互动性的特点;了解在Linux和Windows中安装Python的方法;了解IDLE、PyCharm和Jupyter三种常用Python程序编辑环境。工欲善其事,必先利其器,通过对本章的学习,学习者可拥有一个强大的编程工具,从此开启数据分析编程之旅。 第2章Python语言基础。Python作为一门计算机交流的编程语言,有着跟自然语言相似的特点:字、词、句、段落、篇章,以及相应的行文语法规则。学习本章,要理解程序行文的字词句,主要包括基本数据类型、常量和变量、运算符和表达式;理解程序的段落和篇章,主要包括常用内置函数、库函数和系统函数的使用;掌握程序的语法规则,主要包括常用的变量定义和标识符命名规则、语句组织成文编码规则等。这些都是程序设计的基础,学习者只有对此熟练掌握后,才能在后续的学习中得心应手。 第3章Python组合数据类型。组合数据类型是Python语言区别于其他高级编程语言的一大特色,通过组合数据类型,省去了其他语言各种复杂数据结构的计,给编程人员带来了极大的方便,这也是Python流行于数据分析领域的原因之一。学习本章,要熟练掌握Python组合数据类型(列表、元组、字符串、字典、集合)的创建、访问和常见基本操作,以及序列解包功能。 第4章 Python控制结构。针对物质随时间由简单向复杂、由低级向高级发展的顺序,Python语言有相应的顺序结构语句;针对物质运动发展的条件性,Python语言有相应的选择结构语句;针对物质运动的波浪式前进螺旋式上升规律,Python语言有相应的循环结构语句。学习本章,要从马克思主义自然哲学视角理解Python语言在描述物质运动规律时的表达方式;掌握用Python语言描述常用算法,解决一些基本问题的方式。 第5章 Python函数模块。有些经常用到的能实现特定功能的代码块,我们总是不希望每次用到时都重写一遍,甚至不希望复制一遍,但又想重复使用。Python里这些经常重用的代码块以函数(Function)的形式被定义,每一次复用被称为函数调用,计算机依然要执行重用的代码。学习本章,要理解函数的概念,掌握定义函数的方法,深刻理解函数调用中参数的传递(值传递、地址传递),理解变量的作用域(变量的作用范围或变量的生命周期),理解函数集合模块、包等概念,掌握模块和包的创建及使用方法。 第6章 Python面向对象程序设计。面向过程的程序设计方法难以保证程序的安全性和代码的可重用性,而面向对象的程序设计方法能够更好地提高大型程序的质量和开发效率,增强程序的安全性和提高代码的可重用性。学习本章,重在理解面向对象程序设计思想、类和对象的概念
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值