当前位置:文档之家› 《python》期末考试卷A及答案

《python》期末考试卷A及答案

北京邮电大学2016——2017学年第一学期《Python》期末考试试题 B一、问答题(每题2分,共20分)1、Python 语言的数值类型有哪些? 2分2、请给出乘法、幂运算和求余运算的运算符? 2分3、请解释一下/和//号的区别? 2分4、请给出算术运算的优先级顺序? 2分5、请给出数字转字符串的函数、字符串转整数的函数、字符串转浮点数的函数以及对字符串描述的运算式求值函数?2分6、请给出以读方式、写方式和追加方式打开文件的语法,例如文件名为”file.txt”? 2分7、请给出逻辑运算中的与运算、或运算和非运算的运算符? 2分8、如果从列表list1=[1,4,7,3,8,9]中得到子列表sublist=[7,3],请问如何操作? 2分9、请给出break语句和continue语句的定义及区别? 2分10、请解释lambda表达式的作用,并举例?2分二、给出下列代码行相应的输出(每题3分,共15分)1、str1=“在随机选择的人群中有 {0:d} 个人,” + \“两个人有相同生日的概率是\n {1:.2f}.” print(str1.format(23, .507397))2、L=[“sentence”,“contains”,“five”,“words.”]L.insert(0,“This”)print(“”.join (L))del L[3]L.insert(3,“six”)L.insert(4,“different”)print(“”.join (L))3、letter = input(“enter a,b,or c: ”)letter = letter.upper()if letter ==“a”:print(“a,my name is Alice.”)elif letter ==“b”:print(“to be, or not to be.”)elif letter ==“c”:print(“oh, say, can you see.”)else:print(“you did not enter a valid leeter”)(假设输入的是b)4、num_of_sibilants = 0word =“stargazers”for ch in word:if (ch ==’s’) or (ch ==’z’)num_of_sibilants += 1print(num_of_sibilants)5、def main():p=float(input(“enter the population growth as a percent: ”))print(“人群数要翻倍”,end=””)print(“在大约 {0:.2f} 年后.”.format(doublingTime(p)))def doublingTime(x):time = 72/xreturn time当输入是2时,求输出结果。

三、找出代码中的错误(每题3分,共15分)1、title = (“the”, “call”, “of”, “the”, “wild”)title[1] = “calm”print(“” . join(title))2、## display the elements of a listlist1 = [‘a’,‘b’,‘c’,‘d’]i = 0while i < (len(list1)-1):i += 1print(list1[i])3、list1 = [2, 5, 7, 2, 7, 8]list2 = [ ]for item in list1:if item not in list2:list2.append(item)print list24、##假设文件ABC.txt包含了三行数据a、b、cinfile = open(“ABC.txt”,‘r’)infile.close()line = infile.readline()5、class Triangle:def __init__(base, altitude):self._base = baseself._altitude = altitude四、使用for 循环重写以下程序(10分)num = 1while num <= 9:print(num)num += 2五、编程题(20分)1、文件USPres.txt 包含了以任职时间安排的前44任总统的名字。

编写一个程序,将这些程序放在一个列表中,按照总统姓氏的字母顺序进行排序,并显示列表中前6位总统的名字。

如图1所示。

(10分)图1 列表输出2、如果一个房屋贷款本金为A ,利率为r %,贷款期限为n 年,则每个月还款额的计算公式为:每月还款额=??1-(1+??)-12??×?? ,??=??1200创建一个名为Mortgage 的类,其中包含principal 、interest rate 和term 实例变量以及一个名为calculateMonthlyPayment 的方法。

(10分)六、阐述题(20分)1、请解释一下什么是函数,什么是类和对象,它们有什么关系,并给出一个函数和类的例子。

(20分)函数:将复杂问题分解为若干个子问题的功能块。

类:将数据和操作封装在一起,对一类事物的抽象描述称为类。

对象:对象是类的实例化。

函数、类和对象的例子只要满足定义语法即可,不用很复杂。

John AdamsJohn Q.AdamsChester ArthurJames BuchananGeorge H. W. BushGeorge W. Bush《Python》期末考试试题A卷答案一、问答题(每题2分,共20分)(1)整形和浮点型(或者int和float)(2)乘法*, 幂运算 **,求余 %(3)/是算术除,包含小数,//是整除,不包含小数(意思对即可)(4)1、括号内优先2、幂运算3、乘法、除法、求余运算4、加法减法5、关系运算6、逻辑运算答出前4条即可(5)数字转字符串的函数str(),字符串转数字的函数int(),字符串转浮点数的函数float(),字符串描述的运算式求值函数是eval()(6)读方式:open(“file.txt”, ‘r’)写方式:open(“file.txt”, ‘w’)追加方式:open(“file.txt”, ‘a’)(7)逻辑运算符与:and或运算:or非运算:not。

(8)sublist = list1[2:4](9) break语句表示要终止循环。

continue语句表示结束当前循环,开始下一次循环。

两者的区别是:break终止循环,continue还有继续循环,只是结束本次循环。

(10) lambda表达式的作用就是为了节省函数的格式化定义,同时减少函数调用的开销。

例如 lambda x:x+1二、给出下列代码行相应的输出(每题3分,共15分)1、在一个随机选择群组中有23 个人,有两个人有相同生日的概率是\n 0.51.2、This sentence contains five wordsThis sentence contains six different words3、to be, or not to be.4、35、enter the population growth as a percent:2the population will double in about 36.00 years.二、找出代码中的错误(每题3分,共15分)意思对即可1、第二行无效。

元组中的项不能直接被重新分配值。

2、i应该被初始化为-1以遍历所有的元素。

3、输出函数的调用缺少括号。

4、文件不可读,因为它已经关闭。

5、self参数从第二行缺失。

三、使用for循环重写以下程序(10分)for num in range(1,10,2):print(num)四、编程题(60分)1、def main():##display presidents ordered by length of first name.infile=open(“Uspres.txt”,’r’)listPres=[pres.restrip() for pres in infile]infile.close()listPres.sort(key=sortByLengthOfFirstName)for i in range(6):print(listPresp[i])def sortByLengthOfFirstName(pres)return len(pres.split()[0])main()2、class Mortgage:def __init__(self,principal,interestRate,term):self._principal=principalself._ interestRate= interestRateself._ term= termdef calculateMonthlyPayment(self)i= self._ interestRate/1200return((i/(1-(1+i)**(-12* self._ term))))* self._principal)六、阐述题(20分)1、请解释一下什么是函数,什么是类和对象,它们有什么关系,并给出一个函数和类的例子。

(20分)。

相关主题