可爱的python习题答案status校对lizzie完成度100%CDays-51.计算今年是闰年嘛?判断闰年条件,满足年份模400为0,或者模4为0但模100不为0.o源代码Togglelinenumbers1#coding:utf-82'''cdays-5-exercise-1.py判断今年是否是闰年3@note:使用了import,time模块,逻辑分支,字串格式化等4'''56importtime#导入time模块7thisyear=time.localtime()[0]#获取当前年份8ifthisyear%400==0orthisyear%4==0andthisyear%1000:#判断闰年条件,满足模400为0,或者模4为0但模100不为09print'thisyear%sisaleapyear'%thisyear10else:11print'thisyear%sisnotaleapyear'%thisyear12o运行截屏2.利用python作为科学计算器。熟悉Python中的常用运算符,并分别求出表达式12*34+78-132/6、(12*(34+78)-132)/6、(86/40)**5的值。并利用math模块进行数学计算,分别求出145/23的余数,0.5的sin和cos值(注意sin和cos中参数是弧度制表示)提醒:可通过importmath;help(math)查看math帮助.o源代码Togglelinenumbers1#coding:utf-82'''cdays-5-exercise-2.py求表达式的值3@note:基本表达式运算,格式化输出,math模块4@see:math模块使用可参考=12*34+78-132/6#表达式计算8y=(12*(34+78)-132)/69z=(86/40)**51011print'12*34+78-132/6=%d'%x12print'(12*(34+78)-132)/6=%d'%y13print'(86/40)**5=%f'%z1415importmath#导入数学计算模块1617a=math.fmod(145,23)#求余函式18b=math.sin(0.5)#正弦函式19c=math.cos(0.5)#余弦函式2021print'145/23的余数=%d'%a22print'sin(0.5)=%f'%b23print'cos(0.5)=%f'%c24o运行截屏3.找出0~100之间的所有素数。o源代码Togglelinenumbers1#coding:utf-82'''cdays-5-exercise-3.py求0~100之间的所有素数3@note:for循环,列表类型4@see:math模块使用可参考=10010#基本的方法11result1=[]12fornuminrange(2,N):13f=True14forsnuinrange(2,int(sqrt(num))+1):15ifnum%snu==0:16f=False17break18iff:19result1.append(num)20printresult12122#更好的方法23result2=[pforpinrange(2,N)if0notin[p%dfordinrange(2,int(sqrt(p))+1)]]24printresult225o运行截屏CDays-41.os模块中还有哪些功能可以使用?--提示使用dir()和help()oos模块中还有很多功能,主要的有以下些:os.error,os.path,os.popen,os.stat_result,os.sys,os.system等等等,详细可参见dir(os)和Python帮助文档help(os)2.open()还有哪些模式可以使用?oopen()有以下几种模式:'r':以只读方式打开已存在文件,若文件不存在则抛出异常。此方式是默认方式'U'或者'rU':Python惯例构造了通用换行支持;提供'U'模式以文本方式打开一个文件,但是行可能随时结束:Unix的结束符规定为'\n',苹果系统则为'\r',还有Windows规定为'\r\n',所有这些规定在Python程序中统一为'\n'.'w':以可写方式打开存在或者不存在的文件,若文件不存在则先新建该文件,若文件存在则覆盖该文件'a':用于追加,对unix系统而言,所有的内容都将追加到文件末尾而不管指针的当前位置如何'b':以二进制方式打开。打开一个二进制文件必须用该模式。增加'b'模式是用来兼容系统对当二进制和文本文件的处理不同'r+','w+'和'a+'以更新方式打开文件(注意'w+'覆盖文件)3.尝试for..in..循环可以对哪些数据类型进行操作?ofor..in循环对于任何序列(列表,元组,字符串)都适用。但从广义说来可以使用任何种类的由任何对象组成的序列4.格式化声明,还有哪些格式可以进行约定?o格式化申明o详细:(精巧地址:)dSignedintegerdecimal.iSignedintegerdecimal.oUnsignedoctal.uUnsigneddecimal.xUnsignedhexadecimal(lowercase).XUnsignedhexadecimal(uppercase).eFloatingpointexponentialformat(lowercase).EFloatingpointexponentialformat(uppercase).fFloatingpointdecimalformat.FFloatingpointdecimalformat.gFloatingpointformat.Usesexponentialformatifexponentisgreaterthan-4orlessthanprecision,decimalformatotherwise.GFloatingpointformat.Usesexponentialformatifexponentisgreaterthan-4orlessthanprecision,decimalformatotherwise.cSinglecharacter(acceptsintegerorsinglecharacterstring).rString(convertsanypythonobjectusingrepr()).sString(convertsanypythonobjectusingstr()).%Noargumentisconverted,resultsina%characterintheresult.5.现在的写入文件模式好嘛?有改进的余地?oCDay-4-5.py好在哪里?Togglelinenumbers1#coding:utf-823importos45export=6forroot,dirs,filesinos.walk('/media/cdrom0'):7export+=\n%s;%s;%s%(root,dirs,files)8open('mycd2.cdc','w').write(export)9oCDay-4-6.py又更加好在哪里?Togglelinenumbers1#coding:utf-823importos45export=[]6forroot,dirs,filesinos.walk('/media/cdrom0'):7export.append(\n%s;%s;%s%(root,dirs,files))8open('mycd2.cdc','w').write(''.join(export))9oCDay-4-5.py中使用了字符串的+连接,而CDay-4-6.py中是利用join。字符串的join要比+操作效率高。因为对象的反复+,比一次性内建处理,要浪费更多的资源。6.读取文件cdays-4-test.txt内容,去除空行和注释行后,以行为单位进行排序,并将结果输出为cdays-4-result.txt。ocdays-4-test.txto#somewordsooSometimesinlife,oYoufindaspecialfriend;oSomeonewhochangesyourlifejustbybeingpartofit.oSomeonewhomakesyoulaughuntilyoucan'tstop;oSomeonewhomakesyoubelievethattherereallyisgoodintheworld.oSomeonewhoconvincesyouthattherereallyisanunlockeddoorjustwaitingforyoutoopenit.oThisisForeverFriendship.owhenyou'redown,oandtheworldseemsdarkandempty,oYourforeverfriendliftsyouupinspiritsandmakesthatdarkandemptyworldosuddenlyseembrightandfull.oYourforeverfriendgetsyouthroughthehardtimes,thesadtimes,andtheconfusedtimes.oIfyouturnandwalkaway,oYourforeverfriendfollows,oIfyouloseyouway,oYourforeverfriendguidesyouandcheersyouon.Yourforeverfriendholdsyourhandandtellsyouthateverythingisgoingtobeokay.o源代码Togglelinenumbers1#coding:utf-82'''cdays-4-exercise-6.py文件基本操作3@note:文件读取写入,列表排序,字符串操作4@see:字符串各方法可参考hekp(str)或Python在线文档=open('cdays-4-test.txt','r')#以读方式打开文件8result=list()9forlineinf.readlines():#依次读取每行10line=line.strip()#去掉每行头尾空白11ifnotlen(line)orline.startswith('#'):#判断是否是空行或注释行12continue#是的话,跳过不处理13result.append(line)#保存14result.sort()#排序结果15printresult16open('cdays-4-result.txt','w').write('%s'%'\n'.join(result))#保存入结果文件17o运行截屏CDays-31.根据DiPy10.6.处理命令行参数(精巧地址:)使用getopt.getopt()优化当前功能函式。o源代码Togglelinenumbers1#coding=utf-82'''LovelyPyth