可爱的python习题答案.docxCDays-5
计算今年是闰年嘛?判断闰年条件,满足年份模400为0,或者模4为。但模100不为0.
o源代码
切换行号显示
1 #cod i ng:utf-8
2 ' ' ' cdays-5-exerci se-1. py判断今年是否是闰年
3 ***@note:使用了 import, time模块,逻辑分支,字串格式化等
_4 一,
_5
6 import time #导入 time 模块
7 th i syear = t i me. I oca 11 i me () [0] #获取当前年份
—8 if thisyear % 400 == 0 or thisyear % 4 ==0 and thisyear % 100 <> 0: #判断闰年条件,满足模 400 为 0,或者模 4 为 0 但模 100 不为 0
9 pr int 'this year %s is a leap year' % thisyear
〔0 else:
pr int 'this year %s i s not a Ieap year' % th i syear
o 运行截屏
In [1]: import time
In [2]: thisyear = ()[0]
In [3]: if thisyear % 400 == 0 or thisyear % 4 == 0 and thisyear % 100 <> 0: print 'this year %s is a leap year' % thisyear
...:else:
print 'this year %s is not a leap year* % thisyear
this year 2008 is a leap year
利 用python作为科学计算器。熟悉Python中的常用运算符,并分别求出表达式12*34+78-132/6、(12*(34+78)-132) /6、(86/40)**5的 值。并利用math模块进行数学计算,分别求出145/23的余数,(注意sin和cos中参数是 弧度制表示)提醒:可通过 import math; he I p ("math")查看 math 帮助.
o源代码
切换行号显示
1 #cod i ng:utf-8
2 cdays-5-exerc i se_2. py 求表达式的值
3 ***@note:基本表达式运算,格式化输出,math模块
4 ***@see: math 模块使用可参考 http: //docs, python, org/l ib/modu I e-math. html
x = 12*34+78-132/6 # 表达式计算
y = (12*(34+78)-132) /6
z = (86/40)**5
print '12*34+78-132/6 = %d' % x print 1 (12*(34+78)-132)/6 = %d' % y print ' (86/40)**5 = %f' % z
import math
#导入数学计算模块
23)
#求余函式
#正弦函式
#余弦函式
a = math, fmod (145, b = math. sin(0. 5) c = math, cos (0. 5) print ' 145/23 的余数二%d' % a pr i nt ' s i n (0. 5) = %f' %b pr i nt ' cos (0. 5) = %f' %c
运行截屏
In [1]:
x = 12*34+78-132/6
#表达式计算
In [2]:
y = (12*(34+78)-132)/6
In [3]:
z = (86/40)**5
In [4]: 12*34+78
print ,12*34+78-132/6 = %d' % x 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 (86/40)**5 =
'% z
In [7]:
import math
#导入数学计算模块
In [8]:
a = (145, 23)
#求余函式
In [9]:
b = ()
#正弦函式
In [10]:
c = ()
#余弦函式
In [11] : print '145
可爱的python习题答案 来自淘豆网m.daumloan.com转载请标明出处.