status |
校对 |
完成度70% |
1. 故事练习解答
1.1. CDays
1.1.1. CDays-5
- 计算今年是闰年嘛?
- 相关代码:
1 #coding:utf-8
2 '''cdays-5-exercise-1.py this year is a leap year or not
3 @author: U{shengyan<mailto:[email protected]>}
4 @version:$Id$
5 '''
6 import time #导入time模块
7
8 thisyear = time.localtime()[0] #获取当前年份
9
10 if thisyear % 400 == 0 or thisyear % 4 ==0 and thisyear % 100 <> 0: #判断闰年条件,满足模400为0,或者模4为0但模100不为0
11 print 'this year %s is a leap year' % thisyear
12 else:
13 print 'this year %s is not a leap year' % thisyear
利用python作为科学计算器。熟悉Python中的常用运算符,并分别求出表达式12*34+78-132/6、(12*(34+78)-132)/6、(8.6/4)**5的值。并利用math模块进行数学计算,分别求出145/23的余数,0.5的sin和cos值等等,详细可参考help(math)。
- 相关代码:
1 #coding:utf-8
2 '''cdays-5-exercise-2.py basic operation and math library
3 @author: U{shengyan<mailto:[email protected]>}
4 @version:$Id$
5 '''
6
7 x = 12*34+78-132/6 #表达式计算
8 y = (12*(34+78)-132)/6
9 z = (8.6/4)**5
10
11 print '12*34+78-132/6 = %d' % x
12 print '(12*(34+78)-132)/6 = %d' % y
13 print '(8.6/4)**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 print 'cos(0.5) = %f' %c
- 编写程序,在屏幕上打印出如下图案:
attachment:cdays-5-exercise-3.png
- 相关代码:
截图
1.1.2. CDays-4
os 模块中还有哪些功能可以使用? -- 提示使用 dir() help()
open() 还有哪些模式可以使用?
尝试for .. in .. 循环可以对哪些数据类型进行操作?
- 格式化声明,还有哪些格式可以进行约定?
- 现在的写入文件模式好嘛? 有改进的余地?
CDay-4-5.py 好在哪里? :
CDay-4-6.py 又更加好在哪里? :
- 读取文件cdays-4-test.txt内容,去除空行和注释行后,以行为单位进行排序,并将结果输出为cdays-4-result.txt。
1.1.3. CDays-3
1.1.4. CDays-2
1.1.5. CDays-1
1.1.6. CDays-0
1.1.7. CDays+1
1.1.8. CDays+2
1.1.9. CDays+3
1.2. KDays
1.3. 小结
::-- ZoomQuiet [DateTime(2008-04-26T07:41:41Z)] PageComment2