打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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的数组

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
25 条超棒的 Python 一行代码
一篇文章带你弄懂Python异常简介和案例分析
Python if...else 语句的用法
awk 同时处理多个文件
Thinking in Scala vs Erlang
Python for Loop Statements
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服