好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python程序的循环结构

计数循环

>>> for i in range(1,6):

print(i)

1

2

3

4

5

计数循环特定次数

>>> for i in range(1,6,2):

print(i)

1

3

5

字符串遍历循环

>>> for c in "Python123":

print(c,end=",")

P,y,t,h,o,n,1,2,3,

列表遍历循环

>>> for item in [123,"PY",456]:

print(item,end=",")

123,PY,456,

文件遍历循环

import codecs

# 指定使用utf-8字符集读取文件内容

f = codecs.open("test.py", 'r', 'utf-8', buffering=True)

# 使用for循环遍历文件对象

for line in f:

    print(line, end='')

f.close()

无限循环的条件

>>> a=3

>>> while a>0 :

a=a+1

print(a)

循环控制保留字

>>> for c in "PYTHON":

if c == "T":

    continue

print(c,end="")

     else:

        print("正常退出")

PYHON

>>> for c in "PYTHON":

if c=="T":

     break

print(c,end="")

     else:

        print("正常退出")

PY

查看更多关于Python程序的循环结构的详细内容...

  阅读:23次