《Python程序设计》习题与答案-python教材答案.pdf
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《《Python程序设计》习题与答案-python教材答案.pdf》由会员分享,可在线阅读,更多相关《《Python程序设计》习题与答案-python教材答案.pdf(44页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Python 程序设计习题与参考答案第 1 章 基础知识1。1 简单说明如何选择正确的Python 版本。答:在选择 Python 的时候,一定要先考虑清楚自己学习Python 的目的是什么,打算做哪方面的开发,有哪些扩展库可用,这些扩展库最高支持哪个版本的Python,是Python 2.x还是Python3。x,最高支持到 Python 2.7。6 还是 Python 2。7.9。这些问题都确定以后,再做出自己的选择,这样才能事半功倍,而不至于把大量时间浪费在Python 的反复安装和卸载上。同时还应该注意,当更新的 Python 版本推出之后,不要急于更新,而是应该等确定自己所必须使用的
2、扩展库也推出了较新版本之后再进行更新。尽管如此,Python 3毕竟是大势所趋,如果您暂时还没想到要做什么行业领域的应用开发,或者仅仅是为了尝试一种新的、好玩的语言,那么请毫不犹豫地选择Python 3.x 系列的最高版本(目前是 Python 3.4。3).1.2 为什么说 Python 采用的是基于值的内存管理模式?答:Python 采用的是基于值的内存管理方式,如果为不同变量赋值相同值,则在内存中只有一份该值,多个变量指向同一块内存地址,例如下面的代码。x=3 id(x)10417624 y=3 id(y)10417624 y=5 id(y)10417600 id(x)104176241
3、.3 在 Python 中导入模块中的对象有哪几种方式?答:常用的有三种方式,分别为import 模块名 as 别名from 模块名 import 对象名 as 别名from math import*1.4 使用 pip 命令安装 numpy、scipy 模块。答:在命令提示符环境下执行下面的命令:pip install numpypip install scipy1。5 编写程序,用户输入一个三位以上的整数,输出其百位以上的数字。例如用户输入 1234,则程序输出 12.(提示:使用整除运算。)答:1)Python 3.4.2 代码:x=input(Please input an integ
4、er of more than 3 digits:)try:x=int(x)x=x/100if x=0:print(You must input an integer of more than 3 digits.)else:print(x)except BaseException:print(You must input an integer。)2)Python 2.7。8 代码:import typesx=input(Please input an integer of more than 3 digits:)if type(x)!=types.IntType:print You must
5、input an integer.elif len(str(x)!=4:print You must input an integer of more than 3 digits。else:print x/100第 2 章 Python 数据结构2。1 为什么应尽量从列表的尾部进行元素的增加与删除操作?答:当列表增加或删除元素时,列表对象自动进行内存扩展或收缩,从而保证元素之间没有缝隙,但这涉及到列表元素的移动,效率较低,应尽量从列表尾部进行元素的增加与删除操作以提高处理速度.2.2 编写程序,生成包含 1000 个 0 到 100 之间的随机整数,并统计每个元素的出现次数。(提示:使用集合。
6、)答:1)Python 3.4.2 代码import randomx=random.randint(0,100)for i in range(1000)d=set(x)for v in d:print(v,:,x。count(v)2)Python 2。7.8 代码import randomx=random。randint(0,100)for i in range(1000)d=set(x)for v in d:print v,:,x。count(v)2.3 编写程序,用户输入一个列表和 2 个整数作为下标,然后输出列表中介于2 个下标之间的元素组成的子列表。例如用户输入1,2,3,4,5,6和
7、 2,5,程序输出3,4,5,6.答:1)Python 3。4.2 代码x=input(Please input a list:)x=eval(x)start,end=eval(input(Please input the start position and the end position:))print(xstart:end)2)Python 2。7.8 代码x=input(Please input a list:)start,end=input(Please input the start position and the end position:)print xstart:end2
8、.4 设计一个字典,并编写程序,用户输入内容作为键,然后输出字典中对应的值,如果用户输入的键不存在,则输出“您输入的键不存在!答:1)Python 3。4.2 代码d=1:a,2:b,3:c,4:d v=input(Please input a key:)v=eval(v)print(d.get(v,您输入的的键不存在))2)Python 2。7.8 代码d=1:a,2:b,3:c,4:dv=input(Please input a key:)print(d。get(v,您输入的的键不存在)2。5 编写程序,生成包含 20 个随机数的列表,然后将前 10 个元素升序排列,后 10 个元素降序排
9、列,并输出结果。答:1)Python 3。4.2 代码import randomx=random。randint(0,100)for i in range(20)print(x)y=x0:10y。sort()x0:10=yy=x10:20y.sort(reverse=True)x10:20=yprint(x)2)Python 2.7。8 代码import randomx=random.randint(0,100)for i in range(20)print xy=x0:10y.sort()x0:10=yy=x10:20y。sort(reverse=True)x10:20=yprint x2。
10、6 在 Python 中,字典和集合都是用一对大括号作为定界符,字典的每个元素有两部分组成,即键和值,其中键不允许重复。2。7 假设有列表 a=name,age,sex和 b=Dong,38,Male,请使用一个语句将这两个列表的内容转换为字典,并且以列表 a 中的元素为键,以列表 b 中的元素为值,这个语句可以写为c=dict(zip(a,b)。2.8 假设有一个列表 a,现要求从列表 a 中每 3 个元素取 1 个,并且将取到的元素组成新的列表 b,可以使用语句b=a:3.2.9 使用列表推导式生成包含10 个数字 5 的列表,语句可以写为5 for i inrange(10)。2。10
11、不可以(可以、不可以)使用 del 命令来删除元组中的部分元素。第 3 章 选择结构与循环结构3。1 分析逻辑运算符“or”的短路求值特性。答:假设有表达式“表达式1 or 表达式 2,如果表达式1 的值等价于 True,那么无论表达式 2 的值是什么,整个表达式的值总是等价于True。因此,不需要再计算表达式2 的值.3。2 编写程序,运行后用户输入4 位整数作为年份,判断其是否为闰年.如果年份能被400 整除,则为闰年;如果年份能被 4 整除但不能被 100 整除也为闰年。答:1)Python 3.4。2 代码x=input(Please input an integer of 4 dig
12、its meaning the year:)x=eval(x)if x400=0 or(x%4=0 and not x100=0):print(Yes)else:print(No)2)Python 2。7。8 代码x=input(Please input an integer of 4 digits meaning the year:)if x%400=0 or(x%4=0 and not x100=0):print Yeselse:print No3.3 编写程序,生成一个包含50 个随机整数的列表,然后删除其中所有奇数。(提示:从后向前删.)答:1)Python 3.4.2 代码impor
13、t randomx=random.randint(0,100)for i in range(50)print(x)i=len(x)-1while i=0:if xi%2=1:del xii=1print(x)2)Python 2.7.8 代码把上面的代码中第三行和最后一行改为print x 即可。34 编写程序,生成一个包含 20 个随机整数的列表,然后对其中偶数下标的元素进行降序排列,奇数下标的元素不变。(提示:使用切片。)答:1)Python 3。4.2 代码import randomx=random。randint(0,100)for i in range(20)print(x)y=x:
14、2y.sort(reverse=True)x:2=yprint(x)2)Python 2。7.8 代码把上面的代码中第三行和最后一行改为print x 即可。35 编写程序,用户从键盘输入小于1000 的整数,对其进行因式分解。例如,10=25,60=2235。答:1)Python 3。4。2 代码x=input(Please input an integer less than 1000:)x=eval(x)t=xi=2result=while True:if t=1:breakif ti=0:result.append(i)t=t/ielse:i+=1Print x,=,。join(map
15、(str,result)2)Python 2.7。8 代码x=input(Please input an integer less than 1000:)t=xi=2result=while True:if t=1:breakif t%i=0:result。append(i)t=t/ielse:i+=1print x,=,*。join(map(str,result)3.6 编写程序,至少使用2 种不同的方法计算 100 以内所有奇数的和。答:Python 3。4。2 代码如下,如果使用Python 2。7.8 只需要把其中的 print()函数改为 print 语句即可。x=i for i i
16、n range(1,100)if i2=1print(sum(x)print(sum(range(1,100)::2))3。7 编写程序,实现分段函数计算,如下表所示。xx00=x55=x1010=x2020=xy0 x3x-50.5x-20答:Python 3.4。2 代码如下,如果使用Python 2。7.8 只需要把其中的 print()函数改为print 语句即可。x=input(Please input x:)x=eval(x)if x0 or x=20:print(0)elif 0=x5:print(x)elif 5=x10:print(3x5)elif 10=x20:print(
17、0。5*x-2)第 4 章 字符串与正则表达式4.1 假设有一段英文,其中有单独的字母“I误写为“i”,请编写程序进行纠正。答:这里给出Python 3.4.2 代码,如果使用Python 2.7。8 的话只需要修改其中的print()函数为 print 语句即可。1)不使用正则表达式x=i am a teacher,i am man,and i am 38 years old.I am not a businessman.x=x.replace(i ,I)x=x.replace(i ,I)print(x)2)使用正则表达式x=i am a teacher,i am man,and i am
18、38 years old。I am not a businessman.”import repattern=pile(r(?:wb)i(?:w)while True:result=pattern。search(x)if result:if result.start(0)!=0:x=x:result。start(0)+1+I+xresult。end(0)1:else:x=x:result。start(0)+I+xresult。end(0)-1:else:breakprint(x)4。2 假设有一段英文,其中有单词中间的字母“i误写为“I,请编写程序进行纠正。答:这里给出Python 3.4.2
19、代码,如果使用Python 2.7.8 的话只需要修改其中的print()函数为 print 语句即可。import rex=I am a teacher,I am man,and I am 38 years old。I am not a busInessman.”print(x)pattern=re。compile(r(?:w)I(?:w))while True:result=pattern.search(x)if result:if result。start(0)!=0:x=x:result.start(0)+1+i+xresult.end(0)-1:else:x=x:result。sta
20、rt(0)+i+xresult.end(0)-1:else:breakprint(x)4。3 有一段英文文本,其中有单词连续重复了2 次,编写程序检查重复的单词并只保留一个.例如文本内容为“This is is a desk。,程序输出为“This is a desk.”答:这里给出 Python 3.4.2 代码,如果使用Python 2.7。8 的话只需要修改其中的print()函数为 print 语句即可.1)方法一import rex=This is a a desk。pattern=pile(rb(w+)(s+1)1,b)matchResult=pattern。search(x)x=
21、pattern。sub(matchResult。group(1),x)print(x)2)方法二x=This is a a desk。pattern=pile(r(?Pbw+b)s(?P=f))matchResult=pattern。search(x)x=x。replace(matchResult。group(0),matchResult。group(1))4.4 简单解释 Python 的字符串驻留机制.答:Python 支持字符串驻留机制,即:对于短字符串,将其赋值给多个不同的对象时,内存中只有一个副本,多个对象共享该副本。这一点不适用于长字符串,即长字符串不遵守驻留机制,下面的代码演示了
22、短字符串和长字符串在这方面的区别。a=1234 b=1234 id(a)=id(b)True a=123450 b=1234*50 id(a)=id(b)False4.5 编写程序,用户输入一段英文,然后输出这段英文中所有长度为3 个字母的单词。答:这里给出Python 3。4.2 代码,如果使用Python 2.7。8 的话只需要修改其中的print()函数为 print 语句即可.import rex=input(Please input a string:)pattern=re。compile(rba-zA-Z 3b)print(pattern。findall(x)第 5 章 函数设计与
23、使用5.1 运行 5。3.1 小节最后的示例代码,查看结果并分析原因.答:原因是对于函数的默认值参数只会被处理一次,下次再调用函数并且不为默认值参数赋值时会继续使用上一次的结果,对于列表这样的结构,如果调用函数时为默认值参数的列表插入或删除了元素,将会得到保留,从而影响下一次调用。5。2 编写函数,判断一个整数是否为素数,并编写主程序调用该函数.答:这里给出Python 3.4。2 代码,如果使用Python 2。7.8 的话只需要修改其中的print()函数为 print 语句即可。import mathdef IsPrime(v):n=int(math。sqrt(v)+1)for i in
24、 range(2,n):if vi=0:return Noelse:return Yesprint(IsPrime(37))print(IsPrime(60)print(IsPrime(113)5.3 编写函数,接收一个字符串,分别统计大写字母、小写字母、数字、其他字符的个数,并以元组的形式返回结果。答:这里给出 Python 3.4。2 代码,如果使用 Python 2.7。8 的话只需要修改其中的print()函数为 print 语句即可。def demo(v):capital=little=digit=other=0for i in v:if A=i=Z:capital+=1elif a
25、=i=z:little+=1elif 0 def demo():a=3print a a=5 demo()3 a55。5 编写函数,可以接收任意多个整数并输出其中的最大值和所有整数之和.答:这里给出 Python 3.4.2 代码,如果使用 Python 2.7.8 的话只需要修改其中的 print()函数为 print 语句即可。def demo(*v):print(v)print(max(v)print(sum(v))demo(1,2,3)demo(1,2,3,4)demo(1,2,3,4,5)5.6 编写函数,模拟内置函数sum().答:这里给出 Python 3.4。2 代码,如果使用
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Python程序设计 Python 程序设计 习题 答案 教材
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内