《Python程序设计》习题与答案 联系客服

发布时间 : 星期五 文章《Python程序设计》习题与答案更新完毕开始阅读57d44ce23386bceb19e8b8f67c1cfad6195fe917

《Python 程序设计》习题与参考答案 第1章 基础知识

1.1 简单说明如何选择正确的Python 版本。 答:

在选择Python 的时候,一定要先考虑清楚自己学习Python 的目的是什么,打算做哪方面的开发,有哪些扩展库可用,这些扩展库最高支持哪个版本的Python ,是Python 2.x还是Python 3.x,最高支持到Python 2.7.6还是Python 2.7.9。这些问题都确定以后,再做出自己的选择,这样才能事半功倍,而不至于把大量时间浪费在Python 的反复安装和卸载上。同时还应该注意,当更新的Python 版本推出之后,不要急于更新,而是应该等确定自己所必须使用的扩展库也推出了较新版本之后再进行更新。

尽管如此,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) 10417624

1.3 在Python 中导入模块中的对象有哪几种方式? 答:常用的有三种方式,分别为 import 模块名 [as 别名]

● from 模块名 import 对象名[ as 别名] ● from math import *

1.4 使用pip 命令安装numpy 、scipy 模块。 答:在命令提示符环境下执行下面的命令: pip install numpy pip install scipy

1.5 编写程序,用户输入一个三位以上的整数,输出其百位以上的数字。例如用户输入1234,则程序输出12。(提示:使用整除运算。) 答:

1)Python 3.4.2代码:

x = input('Please input an integer of more than 3 digits:') try:

x = int(x)

x = x//100 if 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 types

x = input('Please input an integer of more than 3 digits:') if type(x) != types.IntType:

print 'You must 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之间的随机整数,并统计每个元素的出现次数。(提示:使用集合。) 答:

1)Python 3.4.2代码 import random

x = [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 random

x = [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]和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(x[start: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 x[start:end]

2.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:'d'}

v = input('Please input a key:')

print(d.get(v,'您输入的的键不存在'))

2.5 编写程序,生成包含20个随机数的列表,然后将前10个元素升序排列,后10个元素降序排列,并输出结果。 答:

1)Python 3.4.2代码 import random

x = [random.randint(0,100) for i in range(20)] print(x) y = x[0:10] y.sort() x[0:10] = y y = x[10:20]

y.sort(reverse=True) x[10:20] = y print(x)

2)Python 2.7.8代码 import random

x = [random.randint(0,100) for i in range(20)] print x y = x[0:10] y.sort() x[0:10] = y y = x[10:20]

y.sort(reverse=True) x[10:20] = y print x

2.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的列表,语句可以写为。

2.10 不可以 (可以、不可以)使用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 digits meaning the year:') x = eval(x)

if x@0==0 or (x%4==0 and not x0==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@0==0 or (x%4==0 and not x0==0): print 'Yes' else:

print 'No'

3.3 编写程序,生成一个包含50个随机整数的列表,然后删除其中所有奇数。(提示:从后向前删。) 答:

1)Python 3.4.2代码 import random

x = [random.randint(0,100) for i in range(50)] print(x) i = len(x)-1 while i>=0: if x[i]%2==1: del x[i] i-=1 print(x)

2)Python 2.7.8代码

把上面的代码中第三行和最后一行改为print x即可。

34 编写程序,生成一个包含20个随机整数的列表,然后对其中偶数下标的元素进行降序