《Python程序设计》练习习题及答案.docx

上传人:b****6 文档编号:12918889 上传时间:2023-06-09 格式:DOCX 页数:95 大小:44.94KB
下载 相关 举报
《Python程序设计》练习习题及答案.docx_第1页
第1页 / 共95页
《Python程序设计》练习习题及答案.docx_第2页
第2页 / 共95页
《Python程序设计》练习习题及答案.docx_第3页
第3页 / 共95页
《Python程序设计》练习习题及答案.docx_第4页
第4页 / 共95页
《Python程序设计》练习习题及答案.docx_第5页
第5页 / 共95页
《Python程序设计》练习习题及答案.docx_第6页
第6页 / 共95页
《Python程序设计》练习习题及答案.docx_第7页
第7页 / 共95页
《Python程序设计》练习习题及答案.docx_第8页
第8页 / 共95页
《Python程序设计》练习习题及答案.docx_第9页
第9页 / 共95页
《Python程序设计》练习习题及答案.docx_第10页
第10页 / 共95页
《Python程序设计》练习习题及答案.docx_第11页
第11页 / 共95页
《Python程序设计》练习习题及答案.docx_第12页
第12页 / 共95页
《Python程序设计》练习习题及答案.docx_第13页
第13页 / 共95页
《Python程序设计》练习习题及答案.docx_第14页
第14页 / 共95页
《Python程序设计》练习习题及答案.docx_第15页
第15页 / 共95页
《Python程序设计》练习习题及答案.docx_第16页
第16页 / 共95页
《Python程序设计》练习习题及答案.docx_第17页
第17页 / 共95页
《Python程序设计》练习习题及答案.docx_第18页
第18页 / 共95页
《Python程序设计》练习习题及答案.docx_第19页
第19页 / 共95页
《Python程序设计》练习习题及答案.docx_第20页
第20页 / 共95页
亲,该文档总共95页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

《Python程序设计》练习习题及答案.docx

《《Python程序设计》练习习题及答案.docx》由会员分享,可在线阅读,更多相关《《Python程序设计》练习习题及答案.docx(95页珍藏版)》请在冰点文库上搜索。

《Python程序设计》练习习题及答案.docx

《Python程序设计》练习习题及答案

《Python程序设计》习题与参照答案

 

第1章基础知识

 

简单说明怎样选择正确的Python版本。

 

答:

 

在选择Python的时候,必定要先考虑清楚自己学习Python的目的是什么,打当作哪

 

方面的开发,有哪些扩展库可用,这些扩展库最高频频安装和卸载上。

同时还应当注意,

 

当更新的Python版本推出以后,不要急于更新,而是应当等确立自己所一定使用的扩展库

 

也推出了较新版本以后再进行更新。

 

只管这样,Python3毕竟是大势所趋,假如您临时还没想到要做什么行业领域的应用

 

开发,或许只是是为了试试一种新的、好玩的语言,那么请绝不踌躇地选择

 

列的最高版本(目前是)。

 

为何说Python采纳的是鉴于值的内存管理模式?

 

 

 

答:

 

Python采纳的是鉴于值的内存管理方式,假如为不一样变量赋值同样值,则在内存中只

 

有一份该值,多个变量指向同一块内存地点,比以下边的代码。

 

>>>x=3

 

>>>id(x)

 

>>>y=3

 

>>>id(y)

 

>>>y=5

 

>>>id(y)

 

>>>id(x)

在Python中导入模块中的对象有哪几种方式?

 

答:

常用的有三种方式,分别为

 

import模块名[as又名]

 

from模块名import对象名[as又名]

 

frommathimport*

 

使用pip命令安装numpy、scipy模块。

 

答:

在命令提示符环境下履行下边的命令:

 

pipinstallnumpy

 

pipinstallscipy

 

编写程序,用户输入一个三位以上的整数,输出其百位以上的数字。

比如用户输

 

入1234,则程序输出12。

(提示:

使用整除运算。

 

答:

 

x=input('Pleaseinputanintegerofmorethan3digits:

')

 

try:

 

x=int(x)

 

x=x//100

 

ifx==0:

 

print('Youmustinputanintegerofmorethan3digits.')

 

else:

 

print(x)

 

exceptBaseException:

print('Youmustinputaninteger.')

 

importtypes

 

x=input('Pleaseinputanintegerofmorethan3digits:

')

 

iftype(x)!

=types.IntType:

 

print'Youmustinputaninteger.'

 

eliflen(str(x))!

=4:

 

print'Youmustinputanintegerofmorethan3digits.'

 

else:

 

printx//100

 

第2章Python数据结构

 

为何应尽量从列表的尾部进行元素的增添与删除操作?

 

答:

 

当列表增添或删除元素时,列表对象自动进行内存扩展或缩短,进而保证元素之间没有空隙,但这波及到列表元素的挪动,效率较低,应尽量从列表尾部进行元素的增添与删除操作以提升办理速度。

 

编写程序,生成包括1000个0到100之间的随机整数,并统计每个元素的出现次

 

数。

(提示:

使用会合。

 

答:

 

importrandom

 

x=[random.randint(0,100)foriinrange(1000)]

 

d=set(x)

 

forvind:

print(v,':

',x.count(v))

 

importrandom

 

x=[random.randint(0,100)foriinrange(1000)]

 

d=set(x)

 

forvind:

 

printv,':

',x.count(v)

 

编写程序,用户输入一个列表和2个整数作为下标,而后输出列表中介于2个下

 

标之间的元素构成的子列表。

比如用户输入[1,2,3,4,5,6]和2,5,程序输出[3,4,5,6]。

 

答:

 

x=input('Pleaseinputalist:

')

 

x=eval(x)

 

start,end=eval(input('Pleaseinputthestartpositionandtheendposition:

'))

 

print(x[start:

end])

 

x=input('Pleaseinputalist:

')

 

start,end=input('Pleaseinputthestartpositionandtheendposition:

')

 

printx[start:

end]

 

设计一个词典,并编写程序,用户输入内容作为键,而后输出词典中对应的值,

 

假如用户输入的键不存在,则输出“您输入的键不存在!

 

答:

 

d={1:

'a',2:

'b',3:

'c',4:

'd'}

 

v=input('Pleaseinputakey:

')

 

v=eval(v)

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

 

d={1:

'a',2:

'b',3:

'c',4:

'd'}

 

v=input('Pleaseinputakey:

')

 

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

 

编写程序,生成包括20个随机数的列表,而后将前10个元素升序摆列,后10个

 

元素降序摆列,并输出结果。

 

答:

 

importrandom

 

x=[random.randint(0,100)foriinrange(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)

 

importrandom

 

x=[random.randint(0,100)foriinrange(20)]

 

printx

 

y=x[0:

10]

 

y.sort()

 

x[0:

10]=y

y=x[10:

20]

 

y.sort(reverse=True)

 

x[10:

20]=y

 

printx

 

 

Python

 

中,词典和会合都是用一对

 

大括号

 

作为定界符,词典的每个元素

有两部分构成,即

键和

值,此中

键不一样意重复。

 

假定有列表a=['name','age','sex']

 

语句将这两个列表的内容变换为词典,而且以列表

 

和b=['Dong',38,'Male']

 

a中的元素为键,以列表

 

,请使用一个

 

b中的元素为

值,这个语句能够写为

c=dict(zip(a,b))

 

假定有一个列表a,现要求从列表a中每3个元素取1个,而且将取到的元素构成

 

新的列表b,能够使用语句b=a[:

:

3]。

 

使用列表推导式生成包括10个数字5的列表,语句能够写为[5foriin

 

range(10)]。

 

不可以够(能够、不可以够)使用del命令来删除元组中的部分元素。

 

第3章选择结构与循环结构

 

剖析逻辑运算符“or”的短路求值特征。

 

答:

 

假定有表达式“表达式1or表达式2”,假如表达式1的值等价于True,那么不论表达式2的值是什么,整个表达式的值老是等价于True。

所以,不需要再计算表达式2的值。

 

编写程序,运转后用户输入

 

被400整除,则为闰年;假如年份能被

 

4位整数作为年份,判断其能否为闰年。

假如年份能

 

4整除但不可以被100整除也为闰年。

答:

 

x=input('Pleaseinputanintegerof4digitsmeaningtheyear:

')

 

x=eval(x)

 

ifx%400==0or(x%4==0andnotx%100==0):

 

print('Yes')

 

else:

 

print('No')

 

x=input('Pleaseinputanintegerof4digitsmeaningtheyear:

')

 

ifx%400==0or(x%4==0andnotx%100==0):

 

print'Yes'

 

else:

 

print'No'

 

编写程序,生成一个包括50个随机整数的列表,而后删除此中所有奇数。

(提示:

 

从后向前删。

 

答:

 

importrandom

 

x=[random.randint(0,100)foriinrange(50)]

 

print(x)

 

i=len(x)-1

 

whilei>=0:

 

ifx[i]%2==1:

 

delx[i]

 

i-=1

print(x)

 

把上边的代码中第三行和最后一行改为printx即可。

 

34编写程序,生成一个包括20个随机整数的列表,而后对此中偶数下标的元素进行降序摆列,奇数下标的元素不变。

(提示:

使用切片。

 

答:

 

importrandom

 

x=[random.randint(0,100)foriinrange(20)]

 

print(x)

 

y=x[:

:

2]

 

y.sort(reverse=True)

 

x[:

:

2]=y

 

print(x)

 

把上边的代码中第三行和最后一行改为printx即可。

 

35编写程序,用户从键盘输入小于1000的整数,对其进行因式分解。

比如,10=2×5,

 

60=2×2×3×5。

 

答:

 

x=input('Pleaseinputanintegerlessthan1000:

')

 

x=eval('x')

 

t=x

 

i=2

 

result=[]

 

whileTrue:

ift==1:

 

break

 

ift%i==0:

 

result.append(i)

 

t=t/i

 

else:

 

i+=1

 

Printx,'=','*'.join(map(str,result))

 

x=input('Pleaseinputanintegerlessthan1000:

')

 

t=x

 

i=2

 

result=[]

 

whileTrue:

 

ift==1:

 

break

 

ift%i==0:

 

result.append(i)

 

t=t/i

 

else:

 

i+=1

 

printx,'=','*'.join(map(str,result))

 

编写程序,起码使用2种不一样的方法计算100之内所有奇数的和。

 

x=[iforiinrange(1,100)ifi%2==1]

print(sum(x))

 

print(sum(range(1,100)[:

:

2]))

 

编写程序,实现分段函数计算,以下表所示。

 

xy

 

x<00

 

0<=x<5x

 

5<=x<103x-5

 

10<=x<20

 

20<=x0

 

x=input('Pleaseinputx:

')

 

x=eval(x)

 

ifx<0orx>=20:

 

print(0)

 

elif0<=x<5:

 

print(x)

 

elif5<=x<10:

 

print(3*x-5)

 

elif10<=x<20:

 

print(0.5*x-2)

第4章字符串与正则表达式

 

假定有一段英文,此中有独自的字母“I”误写为“i”,请编写程序进行纠正。

 

1)不使用正则表达式

 

x="iamateacher,iamman,andiam38yearsold.Iamnotabusinessman."

 

x=x.replace('i','I')

 

x=x.replace('i','I')

 

print(x)

 

2)使用正则表达式

 

x="iamateacher,iamman,andiam38yearsold.Iamnotabusinessman."

 

importre

 

pattern=pile(r'(?

:

[^\w]|\b)i(?

:

[^\w])')

 

whileTrue:

 

result=pattern.search(x)

 

ifresult:

 

ifresult.start(0)!

=0:

 

x=x[:

result.start(0)+1]+'I'+x[result.end(0)-1:

]

 

else:

 

x=x[:

result.start(0)]+'I'+x[result.end(0)-1:

]

 

else:

 

break

 

print(x)

 

假定有一段英文,此中有单词中间的字母“i”误写为“I”,请编写程序进行纠正。

importre

 

x="Iamateacher,Iamman,andIam38yearsold.IamnotabusInessman."

 

print(x)

 

pattern=pile(r'(?

:

[\w])I(?

:

[\w])')

 

whileTrue:

 

result=pattern.search(x)

 

ifresult:

 

ifresult.start(0)!

=0:

 

x=x[:

result.start(0)+1]+'i'+x[result.end(0)-1:

]

 

else:

 

x=x[:

result.start(0)]+'i'+x[result.end(0)-1:

]

 

else:

 

break

 

print(x)

 

有一段英文文本,此中有单词连续重复了

 

2次,编写程序检查重复的单词并只保

留一个。

比如文本内容为“

Thisisisadesk.

”,程序输出为“

Thisisadesk.

 

1)方法一

 

importre

 

x='Thisisaadesk.'

 

pattern=pile(r'\b(\w+)(\s+\1){1,}\b')

 

matchResult=pattern.search(x)

 

x=pattern.sub(matchResult.group

(1),x)

 

print(x)

2)方法二

 

x='Thisisaadesk.'

 

pattern=pile(r'(?

P\b\w+\b)\s(?

P=f)')

 

matchResult=pattern.search(x)

 

x=x.replace(matchResult.group(0),matchResult.group

(1))

 

简单解说Python的字符串驻留体制。

 

答:

 

Python支持字符串驻留体制,即:

关于短字符串,将其赋值给多个不一样的对象时,内

 

存中只有一个副本,多个对象共享该副本。

这一点不合用于长字符串,即长字符串不恪守

 

驻留体制,下边的代码演示了短字符串和长字符串在这方面的差别。

 

>>>a='1234'

 

>>>b='1234'

 

>>>id(a)==id(b)True

>>>a='1234'*50

 

>>>b='1234'*50

 

>>>id(a)==id(b)False

编写程序,用户输入一段英文,而后输出这段英文中所有长度为3个字母的单词。

 

importre

 

x=input('Pleaseinputastring:

')

 

pattern=pile(r'\b[a-zA-Z]{3}\b')

print(pattern.findall(x))

第5章函数设计与使用

 

答:

原由是关于函数的默认值参数只会被办理一次,下次再调用函数而且不为默认值参数赋值时会持续使用前一次的结果,关于列表这样的结构,假如调用函数时为默认值参数的列表插入或删除了元素,将会获得保存,进而影响下一次调用。

 

编写函数,判断一个整数能否为素数,并编写主程序调用该函数。

 

importmath

 

defIsPrime(v):

 

n=int(math.sqrt(v)+1)

 

foriinrange(2,n):

 

ifv%i==0:

 

return'No'

 

else:

 

return'Yes'

 

print(IsPrime(37))

 

print(IsPrime(60))

 

print(IsPrime(113))

 

编写函数,接收一个字符串,分别统计大写字母、小写字母、数字、其余字符的

 

个数,并以元组的形式返回结果。

 

defdemo(v):

 

capital=little=digit=other=0

 

foriinv:

 

if'A'<=i<='Z':

 

capital+=1

elif'a'<=i<='z':

 

little+=1

 

elif'0'<=i<='9':

 

digit+=1

 

else:

 

other+=1

 

return(capital,little,digit,other)

 

x='capital=little=digit=other=0'

 

print(demo(x))

 

在Python程序中,局部变量会隐蔽同名的全局变量吗?

请编写代码进行考证。

 

答案:

会。

 

>>>defdemo():

a=3

printa

 

>>>a=5

 

>>>demo()

 

3

 

>>>a

 

5

 

5.5编写函数,能够接收随意多个整数并输出此中的最大值和所有整数之和。

 

defdemo(*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()。

 

defSum(v):

 

s=0

 

foriinv:

 

s+=i

 

returns

 

x=[1,2,3,4,5]

 

print(Sum(x))

 

x=(1,2,3,4,5)

 

print(Sum(x))

 

编写函数,模拟内置函数sorted()。

 

defSorted(v):

 

t=v[:

:

]

 

r=[]

 

whilet:

 

tt=min(t)r.append(tt)t.remove(tt)

returnr

x=[1,3,5,2,1,0,9,7]

 

print(x)

 

print(Sorted(x))

第6章面向对象程序设计

 

继承节例2中的Person类生成Student类,填写新的函数用来设置学生专业,

 

而后生成该类对象并显示信息。

 

importtypes

 

classPerson(object):

#基类一定继承于object,不然在派生类中将没法使用super()函

 

 

def__init__(self,name='',age=20,sex='man'):

 

self.setName(name)

 

self.setAge(age)

 

self.setSex(sex)

 

defsetName(self,name):

 

ifnotisinstance(name,str):

 

print('namemustbestring.')

 

return

 

self.__name=name

 

defsetAge(self,age):

 

ifnotisinstance(age,int):

 

print('agemustbeinteger.')

 

return

 

self.__age=age

 

defsetSex(self,sex):

 

ifsex!

='man'andsex!

='woman':

 

print('sexmustbe"man"or"woman"')

return

 

self.__sex=sex

 

defshow(self):

 

print(self.__name)

 

print(self.__age)

 

print(self.__sex)

 

classStudent(Person):

 

def__init__(s

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2