可爱的python习题答案.doc可爱的python习题答案
+啄木鸟社区
'} V c/vu7
status 校对 lizzie 完成度 100%
CDays-5
计算今年是闰年嘛?判断闰年条件,满足年份模400为0,或者模4为0但模100 不为0.
o源代码
Togle line numbers
#coding:utf-8
''' cdays-5-exercise-l. py判断今年是否是闰年
***@note:使用了 import, time模块,逻辑分支,字串 格式化等
'''
5
import time #导入 time
模块
thisyear = time, localtime () [0] #获取当
前年份
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
12
o运行截屏
In [1]: import time
In [2]: thisyear = ()[0]
In [3]: if thisyear % 400 == 0 or thisyear % 4 == 0 ...: print 'this year %s is a leap year' % tl
...:else:
...: print 'this year %s is not a leap year'
this year 2008 is a leap year
利用python作为科学计算器。熟悉Python中的常用运算符,并分别求出表达式 12*34+7& 132/6、(12*(34+78)-132)/6、(86/40)**5 的值。并利用 math 模块进行 数学计算,分别求出145/23的余数,(注意sin和cos中参 数是弧度制表示)提醒:可通过import math; help("math")查看math帮助.
o源代码
Toaale line numbers
#coding:utf-8
''' cdays-5-exercise-2. py 求表达式的值
***@note:基本表达式运算,格式化输出,math模块
***@see: math模块使用可参考
http://docs. python, org/lib/module-math. html
'''
x = 12*34+78-132/6 #表达式计算
y = (12*(34+78)-132)/6
z = (86/40)**5 10
print ' 12*34+78-132/6 = %d' % x
print ' (12*(34+78)-132)/6 = %d' % y
print ' (86/40)**5
14
import math
16
a = math, fmod(145,
b = math, sin(0. 5)c = math, cos (0. 5)
:%f' % z
#导入数学计算模块
23) #求余函式
#正弦函式#余弦函式
20
print ' 145/23 的余数=%d' % a
print ' sin (0. 5) = %f' %b
print ' cos (0. 5) = %f' %c
24
In [1]:
x = 12*34+78-132/6
#表达式计算
In [2]:
y = (12*(34+78)-132)/6
In [3]:
z = (86/40)**5
In [4]:
print '12*34+78-132/6 =
:%d' % x
12*34+78
1-132/6 = 464
In [5]:
print '(12*(34+78)-132)/6 = %d' % y
(12*(34+78)-132)/6 = 202
In [6]:
print '(86/40)**5 = %f'
% z
(86/40)**5 =
In [7]:
import math
#导入数学计算模块
In [8]:
a = (145, 23)
#求余函式
In [9]:
b = ()
#正弦函式
In [16]:
c = math.
可爱的python习题答案 来自淘豆网m.daumloan.com转载请标明出处.