6.Python基础 循环

根据vamei前辈的博客学习:

http://www.cnblogs.com/vamei/archive/2012/05/30/2526357.html

for x in ..., for x in range (y), while, break, continue 

#!/usr/bin/env python
############# for ########################
i = 0
for a in [12, 'hello', 2.22, "精忠报国"]:  #注意此处有冒号:
    print ("for:", i,"->:",a)
    i = i + 1
#结果:
#0 ->: 12
#1 ->: hello
#2 ->: 2.22
#3 ->: 精忠报国

############## range() #######################
for idx in range(10):  #注意此处有冒号:,idx是0->9整型数字
    print ("range()->:", idx**2)     #打印0->9的平方

############### while ######################
j = 0
while j < 10:
    print ('while->:', j)
    j = j + 2

############### break #####################
for i in range(20):
    if i == 5:
        break;  #当i等于5时,跳出for循环,循环结束
    print ('break->:', i)

############# continue ###################
for i in range(10):
    if i < 5:
        continue  #i<5为true时,继续循环,不再执行下面的语句
    else:
        print ('continue->:', i)
上面程序保存为.py文件,按F5编译。

posted @ 2015-08-05 22:09  笑面浮屠  阅读(175)  评论(0编辑  收藏  举报