可爱的python习题答案.doc

上传人:枫** 文档编号:543298394 上传时间:2024-02-23 格式:DOC 页数:42 大小:1.03MB
返回 下载 相关 举报
可爱的python习题答案.doc_第1页
第1页 / 共42页
可爱的python习题答案.doc_第2页
第2页 / 共42页
可爱的python习题答案.doc_第3页
第3页 / 共42页
可爱的python习题答案.doc_第4页
第4页 / 共42页
可爱的python习题答案.doc_第5页
第5页 / 共42页
点击查看更多>>
资源描述

《可爱的python习题答案.doc》由会员分享,可在线阅读,更多相关《可爱的python习题答案.doc(42页珍藏版)》请在金锄头文库上搜索。

1、可爱的python习题答案status 校对 lizzie 完成度100% CDays-51. 计算今年是闰年嘛?判断闰年条件, 满足年份模400为0, 或者模4为0但模100不为0. o 源代码 Toggle line numbers 1 #coding:utf-8 2 cdays-5-exercise-1.py 判断今年是否是闰年 3 note: 使用了import, time模块, 逻辑分支, 字串格式化等 4 5 6 import time #导入time模块 7 thisyear = time.localtime()0 #获取当前年份 8 if thisyear % 400 = 0

2、or thisyear % 4 =0 and thisyear % 100 0: #判断闰年条件, 满足模400为0, 或者模4为0但模100不为0 9 print this year %s is a leap year % thisyear 10 else: 11 print this year %s is not a leap year % thisyear 12 o 运行截屏 2. 利用python作为科学计算器。熟悉Python中的常用运算符,并分别求出表达式12*34+78-132/6、(12*(34+78)-132)/6、(86/40)*5的值。并利用math模块进行数学计算,分别

3、求出145/23的余数,0.5的sin和cos值(注意sin和cos中参数是弧度制表示)提醒:可通过import math; help(math)查看math帮助. o 源代码 Toggle line numbers 1 #coding:utf-8 2 cdays-5-exercise-2.py 求表达式的值 3 note: 基本表达式运算, 格式化输出, math模块 4 see: math模块使用可参考http:/docs.python.org/lib/module-math.html 5 6 7 x = 12*34+78-132/6 #表达式计算 8 y = (12*(34+78)-13

4、2)/6 9 z = (86/40)*5 10 11 print 12*34+78-132/6 = %d % x 12 print (12*(34+78)-132)/6 = %d % y 13 print (86/40)*5 = %f % z 14 15 import math #导入数学计算模块 16 17 a = math.fmod(145, 23) #求余函式 18 b = math.sin(0.5) #正弦函式 19 c = math.cos(0.5) #余弦函式 20 21 print 145/23的余数 = %d % a 22 print sin(0.5) = %f %b 23 p

5、rint cos(0.5) = %f %c 24 o 运行截屏 3. 找出0100之间的所有素数。 o 源代码 Toggle line numbers 1 #coding:utf-8 2 cdays-5-exercise-3.py 求0100之间的所有素数 3 note: for循环, 列表类型 4 see: math模块使用可参考http:/docs.python.org/lib/module-math.html 5 6 7 from math import sqrt 8 9 N = 100 10 #基本的方法 11 result1 = 12 for num in range(2, N):

6、13 f = True 14 for snu in range(2, int(sqrt(num)+1): 15 if num % snu = 0: 16 f = False 17 break 18 if f: 19 result1.append(num) 20 print result1 21 22 #更好的方法 23 result2 = p for p in range(2, N) if 0 not in p% d for d in range(2, int(sqrt(p)+1) 24 print result2 25 o 运行截屏 CDays-41. os 模块中还有哪些功能可以使用? -

7、 提示使用 dir()和help() o os模块中还有很多功能,主要的有以下些: os.error, os.path, os.popen, os.stat_result, os.sys, os.system等等等,详细可参见dir(os)和Python帮助文档help(os) 2. open() 还有哪些模式可以使用? o open()有以下几种模式: r: 以只读方式打开已存在文件,若文件不存在则抛出异常。此方式是默认方式 U或者rU: Python惯例构造了通用换行支持;提供U模式以文本方式打开一个文件,但是行可能随时结束:Unix的结束符规定为n,苹果系统则为r,还有Windows规定

8、为rn,所有这些规定在Python程序中统一为n. w: 以可写方式打开存在或者不存在的文件,若文件不存在则先新建该文件,若文件存在则覆盖该文件 a: 用于追加,对unix系统而言,所有的内容都将追加到文件末尾而不管指针的当前位置如何 b: 以二进制方式打开。打开一个二进制文件必须用该模式。增加b模式是用来兼容系统对当二进制和文本文件的处理不同 r+,w+和a+以更新方式打开文件(注意w+覆盖文件) 3. 尝试for . in .循环可以对哪些数据类型进行操作? o for.in循环对于任何序列(列表,元组,字符串)都适用。但从广义说来可以使用任何种类的由任何对象组成的序列 4. 格式化声明,

9、还有哪些格式可以进行约定? o 格式化申明 o 详细:http:/docs.python.org/lib/typesseq-strings.html (精巧地址: http:/bit.ly/2TH7cF) d Signed integer decimal. i Signed integer decimal. o Unsigned octal. u Unsigned decimal. x Unsigned hexadecimal (lowercase). X Unsigned hexadecimal (uppercase). e Floating point exponential format

10、 (lowercase). E Floating point exponential format (uppercase). f Floating point decimal format. F Floating point decimal format. g Floating point format. Uses exponential format if exponent is greater than -4 or less than precision, decimal format otherwise. G Floating point format. Uses exponential

11、 format if exponent is greater than -4 or less than precision, decimal format otherwise. c Single character (accepts integer or single character string). r String (converts any python object using repr(). s String (converts any python object using str(). % No argument is converted, results in a % ch

12、aracter in the result. 5. 现在的写入文件模式好嘛? 有改进的余地? o CDay-4-5.py 好在哪里? Toggle line numbers 1 # coding : utf-8 2 3 import os 4 5 export = 6 for root, dirs, files in os.walk(/media/cdrom0): 7 export+=n %s;%s;%s % (root,dirs,files) 8 open(mycd2.cdc, w).write(export) 9 o CDay-4-6.py又更加好在哪里? Toggle line numb

13、ers 1 # coding : utf-8 2 3 import os 4 5 export = 6 for root, dirs, files in os.walk(/media/cdrom0): 7 export.append(n %s;%s;%s % (root,dirs,files) 8 open(mycd2.cdc, w).write(.join(export) 9 o CDay-4-5.py中使用了字符串的+连接,而CDay-4-6.py中是利用join。字符串的join要比+操作效率高。因为对象的反复+,比一次性内建处理,要浪费更多的资源。 6. 读取文件cdays-4-tes

14、t.txt内容,去除空行和注释行后,以行为单位进行排序,并将结果输出为cdays-4-result.txt。 o cdays-4-test.txto #some wordsoo Sometimes in life,o You find a special friend;o Someone who changes your life just by being part of it.o Someone who makes you laugh until you cant stop;o Someone who makes you believe that there really is good in the w

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号