标签:变量 概述 列表遍历 print 死循环 标识 img tin 字符
目录
遍历某个结构形成的循环运行方式
for <循环变量> in <遍历结构>:
<语句块>
for
和in
组成,完整遍历所有元素后结束fro i in range(N):
<语句块>
range()
函数产生的数字序列,产生循环for i in range(5):
print(i)
0
1
2
3
4
for i in range(5):
print('hello:', i)
hello: 0
hello: 1
hello: 2
hello: 3
hello: 4
fro i in range(M,N,K):
<语句块>
range()
函数产生的数字序列,产生循环for i in range(1, 6):
print(i)
1
2
3
4
5
for i in range(1, 6, 2):
print('hello:', i)
hello: 1
hello: 3
hello: 5
for c in s:
<语句块>
for c in 'python':
print(c, end=',')
p,y,t,h,o,n,
for item in ls:
<语句块>
for item in [123, "PY", 456]:
print(item, end=",")
123,PY,456,
for line in fi:
<语句块>
# fi.txt
优美胜于丑陋
明了胜于隐晦
简洁胜于复杂
for line in fi:
print(line)
优美胜于丑陋
明了胜于隐晦
简洁胜于复杂
由条件控制的循环运行方式
a = 3
while a > 0:
a = a - 1
print(a)
2
1
0
# 死循环, (CTRL + C 退出执行)
a = 3
while a > 0:
a = a + 1
print(a)
for c in "PYTHON":
if c == 'T':
continue
print(c, end=',')
P,Y,H,O,N,
for c in "PYTHON":
if c == 'T':
break
print(c, end=',')
P,Y,
s = "PYTHON"
while s != "":
for c in s:
print(c, end=',')
s = s[:-1]
P,Y,T,H,O,N,P,Y,T,H,O,P,Y,T,H,P,Y,T,P,Y,P,
s = "PYTHON"
while s != "":
for c in s:
if c == 'T':
break
print(c, end=',')
s = s[:-1]
P,Y,P,Y,P,Y,P,Y,P,Y,P,
循环与else
for <变量> in <遍历结构>:
<语句块1>
else:
<语句块2>
while <条件>:
<语句块1>
else:
<语句块2>
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
for…in
遍历循环:计数、字符串、列表、文件…while
无限循环continue
和break
保留字:退出当前循环层次break
有关标签:变量 概述 列表遍历 print 死循环 标识 img tin 字符
原文地址:https://www.cnblogs.com/nickchen121/p/11185033.html