5. 循环
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的数组
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。