《P y t h o n程序设计》习题与参考答案第1章基础知识简单说明如何选择正确的Python版本。
答:在选择Python的时候,一定要先考虑清楚自己学习Python的目的是什么,打算做哪方面的开发,有哪些扩展库可用,这些扩展库最高反复安装和卸载上。
同时还应该注意,当更新的Python版本推出之后,不要急于更新,而是应该等确定自己所必须使用的扩展库也推出了较新版本之后再进行更新。
尽管如此,Python 3毕竟是大势所趋,如果您暂时还没想到要做什么行业领域的应用开发,或者仅仅是为了尝试一种新的、好玩的语言,那么请毫不犹豫地选择Python 系列的最高版本(目前是Python )。
为什么说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 numpypip install scipy编写程序,用户输入一个三位以上的整数,输出其百位以上的数字。
例如用户输入1234,则程序输出12。
(提示:使用整除运算。
)答:x = input('Please input an integer of more than 3 digits:')try:x = int(x)x = xelse:print(x)except BaseException:print('You must input an integer.')import typesx = input('Please input an integer of more than 3 digits:')if type(x) != :print 'You must input an integer.'elif len(str(x)) != 4:print 'You must input an integer of more than 3 digits.'else:print xoin(map(str,result))x = input('Please input an integer less than 1000:')t = xi = 2result = []while True:if t==1:breakif t%i==0:(i)t = t/ielse:i+=1print x,'=','*'.join(map(str,result))编写程序,至少使用2种不同的方法计算100以内所有奇数的和。
x = [i for i in range(1,100) if i%2==1]print(sum(x))print(sum(range(1,100)[::2]))编写程序,实现分段函数计算,如下表所示。
x = input('Please input x:') x = eval(x)if x<0 or x>=20:print(0)elif 0<=x<5:print(x)elif 5<=x<10:print(3*x-5)elif 10<=x<20:print*x-2)第4章字符串与正则表达式假设有一段英文,其中有单独的字母“I”误写为“i”,请编写程序进行纠正。
1)不使用正则表达式x = "i am a teacher,i am man, and i am 38 years am not a businessman."x = ('i ','I ')x = (' i ',' I ')print(x)2)使用正则表达式x = "i am a teacher,i am man, and i am 38 years am not a businessman."import repattern = (r'(:[^\w]|\b)i(:[^\w])')while True:result = (x)if result:if (0) != 0:x = x[:(0)+1]+'I'+x[(0)-1:]else:x = x[:(0)]+'I'+x[(0)-1:]else:breakprint(x)假设有一段英文,其中有单词中间的字母“i”误写为“I”,请编写程序进行纠正。
import rex = "I am a teacher,I am man, and I am 38 years am not a busInessman."print(x)pattern = (r'(:[\w])I(:[\w])')while True:result = (x)if result:if (0) != 0:x = x[:(0)+1]+'i'+x[(0)-1:]else:x = x[:(0)]+'i'+x[(0)-1:]else:breakprint(x)有一段英文文本,其中有单词连续重复了2次,编写程序检查重复的单词并只保留一个。
例如文本内容为“This is is a desk.”,程序输出为“This is a desk.”1)方法一import rex = 'This is a a desk.'pattern = (r'\b(\w+)(\s+\1){1,}\b')matchResult = (x)x = (1),x)print(x)2)方法二x = 'This is a a desk.'pattern = (r'(P<f>\b\w+\b)\s(P=f)')matchResult = (x)x = (0),(1))简单解释Python的字符串驻留机制。
答:Python支持字符串驻留机制,即:对于短字符串,将其赋值给多个不同的对象时,内存中只有一个副本,多个对象共享该副本。
这一点不适用于长字符串,即长字符串不遵守驻留机制,下面的代码演示了短字符串和长字符串在这方面的区别。
>>> a = '1234'>>> b = '1234'>>> id(a) == id(b)True>>> a = '1234'*50>>> b = '1234'*50>>> id(a) == id(b)False编写程序,用户输入一段英文,然后输出这段英文中所有长度为3个字母的单词。
import rex = input('Please input a string:')pattern = (r'\b[a-zA-Z]{3}\b')print(x))第5章函数设计与使用答:原因是对于函数的默认值参数只会被处理一次,下次再调用函数并且不为默认值参数赋值时会继续使用上一次的结果,对于列表这样的结构,如果调用函数时为默认值参数的列表插入或删除了元素,将会得到保留,从而影响下一次调用。
编写函数,判断一个整数是否为素数,并编写主程序调用该函数。
import mathdef IsPrime(v):n = int(v)+1)for i in range(2,n):if v%i==0:return 'No'else:return 'Yes'print(IsPrime(37))print(IsPrime(60))print(IsPrime(113))编写函数,接收一个字符串,分别统计大写字母、小写字母、数字、其他字符的个数,并以元组的形式返回结果。
def demo(v):capital = little = digit = other =0for i in v:if 'A'<=i<='Z':capital+=1elif 'a'<=i<='z':little+=1elif '0'<=i<='9':digit+=1else:other+=1return (capital,little,digit,other)x = 'capital = little = digit = other =0'print(demo(x))在Python程序中,局部变量会隐藏同名的全局变量吗?请编写代码进行验证。
答案:会。
>>> def demo():a=3print a>>> a=5>>> demo()3>>> a5编写函数,可以接收任意多个整数并输出其中的最大值和所有整数之和。
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)编写函数,模拟内置函数sum()。
def Sum(v):s = 0for i in v:s += ireturn sx = [1,2,3,4,5]print(Sum(x))x = (1,2,3,4,5)print(Sum(x))编写函数,模拟内置函数sorted()。
def Sorted(v):t = v[::]r = []while t:tt = min(t)(tt)(tt)return rx = [1,3,5,2,1,0,9,7]print(x)print(Sorted(x))第6章面向对象程序设计继承节例2中的Person类生成Student类,填写新的函数用来设置学生专业,然后生成该类对象并显示信息。