Differences between revisions 3 and 4
Revision 3 as of 2008-04-26 12:55:09
Size: 1861
Editor: lizzie
Comment:
Revision 4 as of 2008-04-26 13:11:35
Size: 3454
Editor: lizzie
Comment:
Deletions are marked like this. Additions are marked like this.
Line 14: Line 14:
    相关代码:
{{{#!python
#coding:utf-8
'''cdays-5-exercise-1.py this year is a leap year or not
    @author: U{shengyan<mailto:[email protected]>}
    @version:$Id$
'''
import time #导入time模块
Line 15: Line 23:
 * 利用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)。 thisyear = time.localtime()[0] #获取当前年份
Line 17: Line 25:
if thisyear % 400 == 0 or thisyear % 4 ==0 and thisyear % 100 <> 0: #判断闰年条件,满足模400为0,或者模4为0但模100不为0
 print 'this year %s is a leap year' % thisyear
else:
 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)。
    相关代码:
{{{#!python
#coding:utf-8
'''cdays-5-exercise-2.py basic operation and math library
    @author: U{shengyan<mailto:[email protected]>}
    @version:$Id$
'''

x = 12*34+78-132/6 #表达式计算
y = (12*(34+78)-132)/6
z = (8.6/4)**5

print '12*34+78-132/6 = %d' % x
print '(12*(34+78)-132)/6 = %d' % y
print '(8.6/4)**5 = %f' % z

import math #导入数学计算模块

a = math.fmod(145, 23) #求余函数
b = math.sin(0.5) #正弦函数
c = math.cos(0.5) #余弦函数

print '145/23的余数 = %d' % a
print 'sin(0.5) = %f' %b
print 'cos(0.5) = %f' %c
}}}
Line 18: Line 59:
attachment:cdays-5-exercise-3.png
    相关代码:
{{{#!python
#coding:utf-8
'''cdays-5-exercise-3.py print out and for expression
    @author: U{shengyan<mailto:[email protected]>}
    @version:$Id$
'''

for index in range(1, 6):
 if index > 3:
  index = 2*3 -index #调整index
 print ' '*(3-index), #输出每行空格个数
 print '*'*(2*index - 1) #输出每行*的个数
}}}

截图

status

校对

ShengYan

完成度70%

TableOfContents

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 #coding:utf-8
   2 '''cdays-5-exercise-3.py print out and for expression
   3     @author: U{shengyan<mailto:[email protected]>}
   4     @version:$Id$
   5 '''
   6 
   7 for index in range(1, 6):
   8         if index > 3:   
   9                 index = 2*3 -index              #调整index
  10         print ' '*(3-index),                    #输出每行空格个数
  11         print '*'*(2*index - 1)                 #输出每行*的个数

截图

1.1.2. CDays-4

  • os 模块中还有哪些功能可以使用? -- 提示使用 dir() help()

  • open() 还有哪些模式可以使用?

  • 尝试for .. in .. 循环可以对哪些数据类型进行操作?

  • 格式化声明,还有哪些格式可以进行约定?
  • 现在的写入文件模式好嘛? 有改进的余地?

CDay-4-5.py 好在哪里? :

   1 # coding : utf-8
   2 import os
   3 export = ""
   4 for root, dirs, files in os.walk('/media/cdrom0'):
   5   export+="\n %s;%s;%s" % (root,dirs,files)
   6 open('mycd2.cdc', 'w').write(export)

CDay-4-6.py 又更加好在哪里? :

   1 # coding : utf-8
   2 import os
   3 export = []
   4 for root, dirs, files in os.walk('/media/cdrom0'):
   5     export.append("\n %s;%s;%s" % (root,dirs,files))
   6 open('mycd2.cdc', 'w').write(''.join(export))
  • 读取文件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

ObpLovelyPython/LpyAttach3answer (last edited 2009-12-25 07:14:24 by localhost)