分享

5. 循环

 黎可图书馆 2014-07-15
1. while 循环
while condition:
statement1
statement2
else
statement3
statement4

2. for 循环
for target in sequences:
statement
else:
statement2

sequences包括:
list, tuple, strings, files

(1) strings
for string in "Like": 
 print(string)

(2) list:
lst = [1, 3, 5, "Like", "test", 12.5, 32.6] 
for value in lst: 
 print(value) 
else:
 print("Out loop")

(3) list 2:
str1 = "I Like Like" 
lst = list(str1) 
for value in lst: 
 print(value) 
else: 
 print("Out loop")

(4) tuples
tuple = (1, 2, 3, 4, 5) 
for tup in tuple: 
 print(tup) 
else: 
 print("Out loop")

(5) 文件
file = open("D:/test/test.txt") 
for content in file.readlines(): 
 print(content) 
else: 
 print("End read")

(6) 循环写入文件(文件拷贝)
file = open("D:/test/test.txt", "r") 
for content in file.readlines(): 
 open("D:/test/test2.txt", "a+").write(content) 
else: 
 print("End read")

3. 额外知识
listNum = range(1, 25)
产生1 - 24的数组

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多