本人近期研究了下Python,所用的书籍主要是Python核心编程(第二版)等,在此期间,我对书后的习题(编码部分)进行了实现,由于此书课后没有答案(英文版的答案也不全,我看的是电子书,不知道买的书后有没有答案),所以,在此把我的实现分享一下,希望对初学者有帮助。由于本人水平有限,在实现的编码中肯定有不合理的地方,希望大虾指出,我及时更正,大家共同交流。
3-12(第三章当时做的时候有些没实现,有时间补上)
'''
@author: Riquelqi
'''
#!/user/bin/env python
import os
ls=os.linesep
def write():
content=[]
#Input a filename.
fname=raw_input("Please input a filename:%s"%ls)
print "Please input content(Quit with 'q'):"
lines=raw_input(">")
while lines!="q":
content.append(lines)
lines=raw_input(">")
afile=open(fname,"w")
afile.writelines("%s%s"%(line,ls)for line in content)
afile.close()
print "DONE!",
def read():
#input a filename
fname=raw_input('Please input a filename:%s'%ls)
#To be sure that the file path exists
if not os.path.exists(fname):
print"File doesn't exists!"
else:
afile=open(fname,'r')
for line in afile.readline():
print line,
print '\nDONE!'
if __name__=='__main__':
input=raw_input("""
|Please input your commend|
--|-------------------------|--
--| r | read |--
--| w | write |--
--| q | quit |--
|Please input your commend|
""" )
while input!='q':
if input=='r':
read()
elif input=='w':
write()
input=raw_input("""
|Please input your commend|
--|-------------------------|--
--| r | read |--
--| w | write |--
--| q | quit |--
|Please input your commend|
""" )
4-6(第四章感觉没有什么,不过以下我的这段练习,有助于4-6题的理解,并且这段代码也说明了很多知识)
'''
@author: Riquelqi
'''
# To import like this is better than import types ,because we will use the property like that types.IntType...
# It will select twice
from types import IntType,FloatType,LongType,ComplexType
def display_Number_Best(number):
print number, 'is',
if isinstance(number,(int,long,float,complex)):
print 'a number of type:',type(number).__name__
else:
print 'not a number at all',
def display_Number_Worst(number):
print number,'is'
if type(number)==type(0):
print 'a number of type:',type(0).__name__
elif type(number)==type(0L):
print 'a number of type:',type(0L).__name__
elif type(number)==type(0.0):
print 'a number of type:',type(0.0).__name__
elif type(number)==type(0.0+0.0j):
print 'a number of type:',type(0.0+0.0j).__name__
else :
print 'not a number at all'
def display_Number_Better(number):
print number,'is'
if type(number) is IntType:
print 'a number of type: int'
elif type(number) is FloatType:
print 'a number of type:float'
elif type(number) is LongType:
print 'a number of type:Long'
elif type(number) is ComplexType:
print 'a number of type:Complex'
else:
print 'not a number at all'
def display_Number_Good(number):
print number,'is'
if type(number) == IntType:
print 'a number of type: int'
elif type(number) == FloatType:
print 'a number of type:float'
elif type(number) == LongType:
print 'a number of type:Long'
elif type(number) == ComplexType:
print 'a number of type:Complex'
else:
print 'not a number at all'
if __name__=='__main__':
display_Number_Best(94)
display_Number_Best(-64)
display_Number_Best(10.3232)
display_Number_Best(777777777777777777777777)
display_Number_Best(10.0+10.0j)
display_Number_Best('qq')
# display_Number_Worst(554454)
# display_Number_Worst(10.11)
# display_Number_Worst(10.0+10.0j)
# display_Number_Worst("sdfsdfsd")
# display_Number_Better(45654)
# display_Number_Better(10.0)
# display_Number_Better(10.0+10.0j)
# display_Number_Better(8888888888888888)
# display_Number_Better('xxx')
# display_Number_Good(21554)
# display_Number_Good(8888888888888888888888888)
# display_Number_Good(10.0)
# display_Number_Good(10.0+10.0j)
# display_Number_Good('my')
分享到:
相关推荐
本文为python核心编程第二版的第 2 ,3 ,5 章课后习题代码,欢迎大家下载
python核心编程第二版第7章习题答案.pdf
Python核心编程(第二版)第十章习题答案.pdf
这是python第二版中文版第二到二十三章的习题答案,希望能帮到有需要的朋友
python编程
这是Python核心编程第二版第十三章类的习题答案,从第一题到第十一题和第十九二十题,包括代码和data,如果有误还请批评指正
Python核心编程(第二版)部分习题答案 全部都是自己写的,有两或三个文件没有写完成,后来就不想写了 其中x.x是抄课文中的代码 x_x 是课后习题代码 课文中的代码有很多错误,在我的文档中都已经改过来了,并调试...
这是我做的Python核心编程第二版第十五章习题的答案,从第一题到29题的所有代码。如果有不足或错误之处,还请前辈们指正
本书是Python开发者的完全指南——针对 Python 2.5全面升级 ·学习专业的Python风格、最佳实践和好的编程习惯; ·加强对Python对象、内存模型和Python面向对象特性的深入理解; ·构建更有效的Web、CGI、互联网...