Python程序设计入门与实战(张毅恒微课版)-习题答案.docx

上传人:太** 文档编号:68021642 上传时间:2022-12-26 格式:DOCX 页数:24 大小:54.02KB
返回 下载 相关 举报
Python程序设计入门与实战(张毅恒微课版)-习题答案.docx_第1页
第1页 / 共24页
Python程序设计入门与实战(张毅恒微课版)-习题答案.docx_第2页
第2页 / 共24页
点击查看更多>>
资源描述

《Python程序设计入门与实战(张毅恒微课版)-习题答案.docx》由会员分享,可在线阅读,更多相关《Python程序设计入门与实战(张毅恒微课版)-习题答案.docx(24页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、Python程序设计入门与实战(微课版) 习题答案第1章习题答案一、选择题1.B2. A 3. D 4. C 5. D二、编程题print(,姓名:小明,) print(年龄:30) print。性别:男,)print (职业:Python开发工程师1) print(地址:广州市黄埔区,) print (电话:print。爱好:唱歌、跳舞、打篮球) print(名言:人生苦短、我用Python)第2章习题答案一、选择题1 .D 2.C3. A 4.C二、填空题1.32. ObK 0x1、 Ooi三、编程题1.a = int(input(“输入第一个数字:”) b = int(input(“输入

2、第二个数字:”) numl=a+bnum2=a-bnum3=a*bnum4=a/bnum5 = a / b print(numl) print(num2) print(num3)二、填空题1. B 2.猫吃鱼 3. age = dogl.getAge()4.new_、 _init5.0 1 del 9 ,6.(1) , , , , class objectABC 说明: 执行super(D, self).init()时,表示要查找init()方法,需要从mro中的D往后找, 先找到A,所以输出“A”。执行super(A, self).init()时,表示要查找init()方法,需要从mro中的

3、A往后找, 先找到B,所以输出“B”表示要查找_init_()方法,表示要查找_init_()方法,需要从mro中的B往后找,执行 super(B, self).init()时, 先找到C,所以输出“C需要从mro中的C往后找,需要从mro中的C往后找,执行 super(B, self).init()时,表示要查找_init_()方法,只有object,但是object中init不执行任何打印语句。所以不输出任何内容。三、编程题class Circle:def_init(self, tup, radius, color): self.center = tup self.radius = rad

4、ius self.color = colordef perimeter(self): ,一计算周长return 3.14 * 2 * self.radiusdef area(self): ,“计算面积 return 3.14 * self.radius * self.radiusif _name_ = _main_1 :circle = Circle(2J2), 8, ”粉色“)print (format (circle, perimet er ()., 1.2f1) print(float(circle.area()1. import mathclass Point(object):def_

5、init_(selfx, y, z): self._x = x self._y = yself, z = zdef getPoint(self): return self.x, self._y, self._z#计算与另一个点的曼哈顿距离的方法 def manhattan_distance(selfp):#获取另一个点的x、y、zy, z = p.getPoint()return abs(self._x - x) + abs(self._y - y) + abs(self._z - z)#计算与另一个点的欧式距离的方法 def euclidean_distance(selfp):#获取另一个点

6、的x、v、zx, y, z = p.getPoint()return math.sqrt(self._x - x) * 2 + (self._y - y) * 2 + (self._z - z) * 2)#计算两个点之间的曼哈顿距离的函数def manhattan_distance(pl p2):xl, yl, zl = pl.getPoint()x2j y2, z2 = p2.getPoint()return abs(xl - x2) + abs(yl - y2) + abs(zl - z2)#计算两个点之间的欧式距离的函数def euclidean_distance(pl p2):xl,

7、ylj zl = pl.getPoint()x2, y2) z2 = p2.getPoint()return math.sqrt(xl-x2)*2 + (yl-y2)*2 + (zl-z2)*2)pl = Point(l, 2, 3)p2 = Point(4, 5, 7)站在p2的站在p2的print(pl.manhattan_distance(p2) # 计算pl到p2的曼哈顿距离 站在pl的 角度看print(p2.manhattan distance(pl) # 计算p2到pl的曼哈顿距离角度看print(manhattan_distance(pl p2) #计算pl与p2之间的曼哈顿距

8、离站在第三者的角度看print(pl.euclidean_distance(p2) # 计算pl到p2的欧式距离print(p2.euclidean_distance(pl) # 计算p2到pl的欧式距离print(euclidean distance(pl p2) # 计算pl与p2之间的欧式距离3.class Point(object):def _init(self, x, y, z): self._x = x self._y = y self._z = zdef getPoint(self):return self._x, self._y, self._zdef _str_(self):

9、return (self._x, self._y, self._z)def _add_(self, other): 重载加法运算符一# other表示另一个点otherXj otherY, otherZ = other.getPoint() newX = self._x + otherX newY = self._y + otherY newZ = self._z + otherZ return Point(newX, newY, newZ)def _sub_(self, other): 重载减法运算符 # other表示另一个点 otherX, otherY, otherZ = other.

10、getPoint() newX = self._x - otherX newY = self._y - otherY newZ = self._z - otherZ return Point(newX, newY, newZ)def _mul_(selfother): 重载乘法运算符 # other表示另一个点 otherX, otherY, otherZ = other.getPoint() newX = self.x * otherXnewY = self._y * otherY newZ = self._z * otherZ return Point(newX, newY, newZ)d

11、ef _floordiv_(self other): 重载除法运算符一# other表示另一个点otherXj otherY, otherZ = other.getPoint() newX = self._x / otherX newY = self._y / otherY newZ = self._z / otherZ return Point(newX, newY, newZ)def _truediv_(self, other): 重载除法运算符一# other表示另一个点otherX, otherY, otherZ = other.getPoint() newX = self._x /

12、otherX newY = self._y / otherY newZ = self._z / otherZ return Point(newX, newY, newZ)if _name_ = 1_main_:pl = Point(l, 2, 3)p2 = Point(4, 5, 6)p3 = pl + p2print(p3)p4 = pl - p2 print(p4)p5 = pl * p2 print(p5)p6 = pl / p2print(p6)p7 = pl / p2 print(p7)第7章习题答案一、选择题.D2. C 3. B 4. A 5.C二、填空题1.42.3、2、33.

13、出现错误三、编程题class Error(Exception):length = 0def _init_(self, str): self.length = len(str)def fun(self):if self.length end=H)f.close()if _name_ = _main_1 : main()1. def main():f = open(,data.txt, content = f.read() content = content.swapcase() print(content)f = open(,data.txt, w)f.write(content)f.close

14、()if _name_ = _main_1 : main()第9章习题答案一、选择题1 .D 2.B3.A二、填空题1.102Aw5-w6|w43. l-9d4,9$三、编程题1. import redef re_test(str):#如果匹配上返回True否则返回False pattern =八-?d+$if re.search(patternJ str): return Truereturn False% ( % ( % ( % ( % ( % ( % (% ( % ( % ( % ( % ( % ( % (3 re_test(-3) 32: reestCSZ) 0 re_test( 0)

15、 -2 re_test(-2) -21, re_test(,-21,) 3.6 re_test(36) 2a re_test(12a1)print字符串$匹配整数的结果为s 1 print (字符串s匹配整数的结果为s 1 print。字符串s匹配整数的结果为s, print (,字符串s匹配整数的结果为s 1 print。字符串s匹配整数的结果为s 1 print (1字符串s匹配整数的结果为s1print (字符串s匹配整数的结果为s 1运行程序,输出结果如下: 字符串3匹配整数的结果为True 字符串32匹配整数的结果为True 字符串。匹配整数的结果为True 字符串-2匹配整数的结果

16、为True 字符串-21匹配整数的结果为True 字符串3.6匹配整数的结果为False 字符串2a匹配整数的结果为False2.import redef re_test(str):#如果匹配上返回True否则返回Falsepattern =八-?d+(.dl,2)?$if re.search(pattern str): return Truereturn Falseprint。字符串s匹配小数的结果为s print。字符串s匹配小数的结果为s, print (字符串s匹配小数的结果为s 1 print (1字符串s匹配小数的结果为s1 print (1字符串s匹配小数的结果为s, print

17、。字符串s匹配小数的结果为s 1 print (1字符串s匹配小数的结果为s print字符串s匹配小数的结果为s print (1字符串s匹配小数的结果为s 1if re.search(pattern str): return Truereturn Falseprint。字符串s匹配小数的结果为s print。字符串s匹配小数的结果为s, print (字符串s匹配小数的结果为s 1 print (1字符串s匹配小数的结果为s1 print (1字符串s匹配小数的结果为s, print。字符串s匹配小数的结果为s 1 print (1字符串s匹配小数的结果为s print字符串s匹配小数的结

18、果为s print (1字符串s匹配小数的结果为s 1% (b, re-testCB1)% (32 re_test(,32,)% (0 re-testC)% (-2; re_test(-2)% (-21 re_test( 1 -211)% (3.6, re_test(3.6)% (3.65, re_test(3.65)% (3.652 re_test(,3.652,)% (2a,re-testCZa1)运行程序,输出结果如下: 字符串3匹配小数的结果为True 字符串32匹配小数的结果为True 字符串0匹配小数的结果为True 字符串-2匹配小数的结果为True 字符串-21匹配小数的结果为

19、True 字符串3.6匹配小数的结果为True字符串3.65匹配小数的结果为True 字符串3.652匹配小数的结果为False 字符串2a匹配小数的结果为False3.import redef re_test(str):an = re.search(八a-z+$, str)if an:print (an. group。,全为小写! ,) else:print(str, 不是全小写!”)if _name = 1_main_1 :re_test(1 pythonhello1)re_test(1pythonHello)re_test(1HelloPython1)4.def replace(str)

20、:num = ”0“: 零 nlM: 一“import re 9.” 一 , 4 * , “5“: “五二 “8“: “八二“9“: “九 return numstr.group()strl = input (请输入数字:”) res = re.sub(d), replace, strl) print(res)5.import redef robot(inputStr):# (1)用户输入“你好”,程序输出“你好”patternl = *你好,result = re.match(patternlJ inputStr) if result:return 1 你好,# (2)用户输入“我要吃xx”

21、,程序输出“今晚吃xx” pattern2 =我要吃(+) result = re.match(pattern2J inputstr) if result:str =今晚吃,+ result .group(l) return str# (3)用户输入“1加2等于几”,程序输出“等于3”(完成简单的加法运算) pattern? = (d+)加(d+)等于几,result = re.match(patterns inputstr) if result:valuel = int(result.group(1) value2 = int(result.group(2) value3 = valuel

22、+ value2 str =等于cT % value3 return str# (4)用户输入其他内容。程序输出“这个我不会” return,这个我不会,if _name_ = 1_main_1 :# 使用正则来实现简单的人机对话while True:inputStr = input (请输入:1) result = robot(inputStr) print(result)运行程序,输出结果如下:请输入:你好你好请输入:我要吃西瓜今晚吃西瓜请输入:10加3等于几等于13请输入:来唱首歌这个我不会6.import re data =岗位:Python开发工程师技术要求:l.一年以上Python

23、开发经验2.掌握 Python 语言 If IV VI#方法1retl = re.sub(r,Jdata)print(retl)#方法二ret2 = re.sub(r, data) print(ret2)7.import redata = . asp?id=19http:/www . . com/good.asp?&id=01 II IV IVres = lambda x: x.group(l)ret = re.sub(rH(http:/.+?/).res, data) print(ret)第10章习题答案一、选择题.D2. B 3. D 4. C二、填空题1.502. cbda3. n-1三

24、、编程题def nFun(*nums):print(num4) print(num5)name = input(请输入名字:”)age = input (”请输入年龄:”)number = input(请输入学号:)print(姓名:,+name+年龄:,+age+学号:number)第3章习题答案一、选择题1. A 2. C 3. B二、填空题1 .4, 3 2, 3T, 2, 32. 7,9, 11,13、130,2,4、0,1 2, 3, 4, 53. (R, 3), (b, 2), (d, 4)三、编程题# 1 .定义两个学生studentl = 1 name1: 1xiaohong1

25、1 score1: Chinese: 81, Math1: 83, 1 English 1: 84student2 = 1 name1: ,xiaowang 1 score1: Chinese: 76, Math1: 77, English*: 87)2 .添加Python课程成绩studentl1 scorePython 1 = 90student21 score11 Python1 = 80 print(studentl)print(student2)3.把xiaowang的数学成绩由77分改成89分student2scoreMath = 89 print(student2)4,对xiao

26、wang同学的课程分数按照从低到高排序输出 score_list = student2score1.values() score_list = list(score_list) score_list_asc = sorted(score_list) print(score_list_asc)if nums:numList = nums0sumNum = sum(numList)maxNum = max(numList)minNum = min(numList)averNum = sum(numList) / len(numList)print(总和:sumNum,平均值:,averNum,最大

27、值:,maxNum,最小 值:minNum)if _name_ = 1_main_1 :numbers = input (请输入一组数字:”)numbers = numbers.split( / ) for x in range(len(numbers): numbersx = int(numbersx) nFun(numbers)2.class Node():def _init_(selfitem): self.item = item self.next = Noneclass LinkList():# 定义含有默认值的结点def _init_(selfnode=None): self._h

28、ead = node if self._head:node.next = self._head# 链表是否为空def is_empty(self):return self._head = None# 链表的长度是多少def length(self):if self.is_empty():return 0else:count = 1cur = self._headwhile cur.next != self._head: count += 1 cur = cur.nextreturn count#遍历输出链表中所有的元素 def printElement(self): cur = self._h

29、ead if self.is_empty(): return None else:while cur.next != self._head: print(str(cur.item)end=n ) cur = cur.nextprint(str(cur.item)# 尾部插入链表元素def append(self, item): cur = self._head node = Node(item) if self.is_empty(): self._head = node node.next = self._head else:#遍历找到链表的尾节点 while cur.next != self

30、._head: cur = cur.next#在尾节点处添加,并指向头节点 cur.next = nodenode.next = self._head#在对应的下标中插新的元素 def insert(selfpos, item): cur = self._head node = Node(item)#判断是尾部头部插入还是尾部插入 if pos = self.length(): self.append(item) else:#定义一个位于cur前的游标 preCur = None # count = 0while count != pos: preCur = cur cur = cur.nex

31、t count += 1node.next = preCur.next preCur.next = node#头部插入新元素 def add(self, item): cur = self._head node = Node(item) if self.is_empty(): self._head = node node.next = self._head else:while cur.next != self._head: cur = cur.nextnode.next = self._head self._head = node cur.next = self._head#移除对应下标的元

32、素 def remove(selfitem): cur = self._head #判断是否为空 if self.is_empty(): returnelse:preCur = Noneif (preCur = None) and (cur.next = self._head): prints整个链表只有一个元素! ”)elif (preCur = None) and (cur.next != self._head): tmp = curwhile tmp.next != self._head: tmp = tmp.nexttmp.next = cur.next self._head = cu

33、r.next else:preCur.next = cur.next#查找是否含有对应的元素 def search(selfitem):cur = self._head if self.is_empty(): return False else:node = Node(item) while cur.item != node.item:if cur.next = self._head: return Falsecur = cur.nextreturn Trueif _name_ = _main_” :#定义单向循环链表linkList = LinkList()#测试append尾部插入元素li

34、nkList.append(1)linkList.append(2)linkList.append(3)linkList.append(4)print (遍历链表中的元素:11) linkList.printElement()print,链表的长度为度 + str (linkList. length()print(1分害【J线1)#测试insert在对应的下标插入元素linkList.insert(0 -1)linkList.insert(1 66)linkList.insert(3666)linkList.insert(66666) print (遍历链表中的元素:) linkList. p

35、rintElement()print。分割线1)# 测试add头部插入新元素linkList.add(-2)linkList.add(-3)print (遍历链表中的元素:11)linkList.printElement()print。分割线1)# 测is_empty链表中的元素是否为空空为:True非空为:Flase print(链表中的元素是否为空:” + str (linkList. is_empty () print (遍历链表中的元素:”) linkList.printElement()printC分割线1)# 测试length链表的中元素的长度print(链表中元素的长度:” +

36、str(linkList.length()printC分割线1)# 测试 search,返回 True 或 False print(str(linkList.search(665) print(str(linkList.search(-2) print(str(linkList.search(3) print(str(linkList . search(1234)print。分割线1)# 测试 removelinkList.remove(-3) print(遍历链表中的元素:”) linkList.printElement()3.class Node:def _init_(self, valu

37、e): self.value = value self.pre = None self.next = Noneclass LinkList:#初始化def _init_(self): self._head = None#遍历输出链表中所有的元素 def printElement(self): head = self._head if self.is_empty(): return Noneelse: while head:print(str(head.value)end=) head = head.next#链表是否为空def is_empty(self):return self._head

38、is None#链表的长度是多少def length(self):count = 0head = self._head while head:count += 1head = head.next return count#头部插入新元素def add(self, value): head = self._head newNode = Node(value) if head is None:self._head = newNode else:newNode.next = head newNode.next.pre = newNode self, head = newNode#尾部插入链表元素de

39、f append(selfvalue): head = self._head newNode = Node(value) if self.is_empty():self._head = newNode else:while head.next:head = head.next head.next = newNode newNode.pre = head# 在对应的下标中插新的元素def insert(selfindex, value): if index self.length(): self.append(value)else:newNode = Node(value)head = self

40、._head count = 0 while count = a_max: a_max = a_sumelif a sum 0:a_sum = 0print(最大子数组和为:为a_max)if _name_ = _main_:main()# My name is Python.工m 29 years old. def main():res = input(请输入字符串:,) abcSum = numSum = spaceSum = otherSum =for i in iter(res): if i.isalpha(): abcSum.append(i) elif i.isspace(): spaceSum.append(i) elif i.isdigit(): numSum.appen

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 应用文书 > 解决方案

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁