Python基础4--一看就会的选择与循环

Posted on 2018-01-28 18:55  _hqc  阅读(104)  评论(0编辑  收藏  举报

 

1 选择

if elif else

注意后面均有:

if age>18:
    print 'adult'
elif age>6:
    print 'teenager'
else:
    print 'kid'

2 循环

1) for x in xx:

names = ['Michael', 'Bob', 'Tracy']
for name in names:
    print name

2)while xx:

sum = 0
n = 99
while n > 0:
    sum = sum + n
    n = n - 2
print sum

3)高级range(a,b,c),a起始,b终止(不包括b),c步长

print sum(range(1,100,2))

3 break和continue

break使整个循环提前结束

continue终止本次循环,忽略continue之后的所有语句,直接回到循环的顶端,提前进入下一次循环