python学习记录(第八天)
时间:2010-11-15 来源:simonchia
if测试
基本例子:
>>> if 1:
... print 'true'
...
true
注意:1为布尔真值
>>> if not 1:
... print 'true'
... else:
... print 'false'
...
false
多路分支
>>> x='kill you'
>>> if x=='kiss you':
... print 'i love you'
... elif x=='bugs':
... print 'what\'s up doc?'
... else:
... print 'run!run!run!'
...
run!run!run!
while循环
简单例子一
>>> x='spam'
>>> while x:
... print x,
... x=x[1:]
...
spam pam am m
简单例子二
>>> while a < b:
... print a,
... a+=1
...
0 1 2 3 4 5 6 7 8 9
continue:
>>> while d:
... d=d-1
... if d%2 !=0:continue
... print d
...
8
6
4
2
0
else:
>>> y=20
>>> x=y/2
>>> while x > 1:
... if y%x == 0 :
... print y,'has factor',x
... break
... x=x-1
... else:
... print y,'is prime'
...
20 has factor 10
相关阅读 更多 +