网站制作学习网Python→正文:Python 中的循环方法
字体:

Python 中的循环方法

Python 2017/12/18 19:35:03  点击:不统计

%77w%77%2Ef%6F%72p%73%70%2Ec%6E
1. # Python中的for循环是很容易的,跟Shell 等其他语言相似,看下面的例子

nameList = ['jack', 'lisa', 'lucy']
for name in nameList:
    print name
    print ' say: hi'
print 'out of for '
# 结束for循环仍然是通过换行顶首格处理。
2.# Python,还有 while 循环。看下面的例子
i = 0
while i < 10:
    print i
    i = i + 1
print i
 
3.# 上面说了循环,之后我们怎么跳出循环,跟其他语言一样用break
while True:
    if i > 20:
        break  # 这里是跳出的条件,for循环里面一样
    i = i + 1
print i
 
4.# 上面说了跳出循环,那继续循环也就出来了,continue
while True:
    if i > 10:
        i = i - 1
        continue
    else:
        print i
        break
 
5.# 多个嵌套循环,则通过 4个空格,进行划分层级
temp = [1, 3, 4]
for i in temp:
    for j in temp:
        print i + j
 

http://%77%77%77%2E%66%6F%72%61%73%70%2E%63%6E

·上一篇:Python 中的ifelse判断 >>    ·下一篇:Python 中的set >>
推荐文章
最新文章