- 62
- 0
- 约2.69万字
- 约 74页
- 2023-09-25 发布于上海
- 举报
《Python 程序设计》习题与参考答案
第 1 章 基础知识
简单说明如何选择正确的 Python 版本。答:
在选择 Python 的时候,一定要先考虑清楚自己学习 Python 的目的是什么,打算做哪方面的开发,有哪些扩展库可用,这些扩展库最高反复安装和卸载上。同时还应该注意, 当更新的 Python 版本推出之后,不要急于更新,而是应该等确定自己所必须使用的扩展库也推出了较新版本之后再进行更新。
尽管如此,Python 3 毕竟是大势所趋,如果您暂时还没想到要做什么行业领域的应用开发,或者仅仅是为了尝试一种新的、好玩的语言,那么请毫不犹豫地选择Python 3.x 系列的最高版本(目前是 Python 3.4.3)。
为什么说 Python 采用的是基于值的内存管理模式? 答:
Python 采用的是基于值的内存管理方式,如果为不同变量赋值相同值,则在内存中只有一份该值,多个变量指向同一块内存地址,例如下面的代码。
x = 3
id(x)
y = 3
id(y)
y = 5
id(y)
id(x)
在 Python 中导入模块中的对象有哪几种方式? 答:常用的有三种方式,分别为
import 模块名 [as 别名]
from 模块名 import 对象名[ as 别名]
from math import *
使用 pip 命令安装 numpy、scipy 模块。答:在命令提示符环境下执行下面的命令: pip install numpy
pip install scipy
编写程序,用户输入一个三位以上的整数,输出其百位以上的数字。例如用户输入 1234,则程序输出 12。(提示:使用整除运算。)
答:
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.) 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 数据结构
为什么应尽量从列表的尾部进行元素的增加与删除操作? 答:
当列表增加或删除元素时,列表对象自动进行内存扩展或收缩,从而保证元素之间没有缝隙,但这涉及到列表元素的移动,效率较低,应尽量从列表尾部进行元素的增加与删除操作以提高处理速度。
编写程序,生成包含 1000 个 0 到 100 之间的随机整数,并统计每个元素的出现次数。(提示:使用集合。)
答 : import random
x = [random.randint(0,100) for i in range(1000)] d = set(x)
for v in d:
print(v, :, x.count(v)) 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 个整数作为下标,然后输出列表中介于 2 个下标之间的元素组成的子列表。例如用户输入[1,2,3,4,5,6]和 2,5,程序输出[3,4,5,6]。
答:
x = input(Please input a list:) x = eval(x)
start, end = eval(input(Please input the start positionand the end position:)) print(x[start:end])
x = input(Please input a list:)
start, end = input(Please input the start position and the end position:) print x[start:end]
设计一个字典,并编写程序,用户输入内容作为键,然后输出字典中对应的值, 如果用户输入的键不
原创力文档

文档评论(0)