Python的循环语句
for循环语句
Python 中的 for 语句是 对任意序列进行迭代(即列表,元组,字典,集合或字符串),条目的迭代顺序与它们在序列中出现的顺序一致
words = ['cat', 'window', 'defenestrate']
for x in words:
print(x, ',', len(x))
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/01.py
cat , 3
window , 6
defenestrate , 12
Process finished with exit code 0
for循环用于推导式
for循环用于列表推导式
lst = [1, 2, 3]
l = [x ** 2 for x in lst]
print(l)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/01.py
[1, 4, 9]
Process finished with exit code 0
for循环用于字典推导式
dsk = {1:"one", 2:"two", 3:"three"}
d = {x for x in dsk}
print(d)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/01.py
{1, 2, 3}
Process finished with exit code 0
对字典数据类型进行for循环迭代时,迭代的是键
for循环迭代序列的副本
在遍历同一个集合时修改该集合的代码可能很难获得正确的结果。通常,更直接的做法是循环遍历该集合的副本或创建新集合
lst = [(1,2), (3,4), (5,6)]
for x, y in lst.copy():
print('x = ', x, ' y = ', y)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/01.py
x = 1 y = 2
x = 3 y = 4
x = 5 y = 6
Process finished with exit code 0
for循环遍历字符串
str = "apple, banana"
for c in str:
print('c = ', c)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/01.py
c = a
c = p
c = p
c = l
c = e
c = ,
c =
c = b
c = a
c = n
c = a
c = n
c = a
Process finished with exit code 0
break语句跳出for循环
lst = ["apple", "banana", "orange"]
for l in lst:
print('l = ', l)
if l == 'banana':
break
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/01.py
l = apple
l = banana
Process finished with exit code 0
continue语句停止当前迭代继续迭代下一个的for循环
lst = ["apple", "banana", "orange"]
for l in lst:
if l == 'banana':
continue
print('l = ', l)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/01.py
l = apple
l = orange
Process finished with exit code 0
for循环迭代range产生的序列
for x in range(5):
print(x)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/01.py
0
1
2
3
4
Process finished with exit code 0
带else分支语句的for循环
for 循环中的 else 关键字指定循环结束时要执行的代码块
for x in range(5):
print(x)
else:
print("Finally finished!")
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/01.py
0
1
2
3
4
Finally finished!
Process finished with exit code 0
嵌套的for循环语句
a = [1, 2, 3]
b = ["one", "two", "three"]
for x in a:
for y in b:
print(x, y)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/01.py
1 one
1 two
1 three
2 one
2 two
2 three
3 one
3 two
3 three
pass语句的for循环
*for 语句不能为空,但是如果您处于某种原因写了无内容的 for 语句,请使用 pass 语句来避免错误。
for x in [0, 1, 2]:
pass
while循环语句
如果使用 while 循环,只要条件为真,我们就可以执行一组语句。
i = 1
while i < 7:
print(i);
i += 1
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/01.py
1
2
3
4
5
6
Process finished with exit code 0
break语句跳出while循环
i = 1
while i < 7:
print(i)
if i == 3:
break
i += 1
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/01.py
1
2
3
Process finished with exit code 0
continue语句继续while循环
i = 0
while i < 7:
i += 1
if i == 3:
continue
print(i)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/01.py
1
2
4
5
6
7
Process finished with exit code 0
带else语句的while循环
通过使用 else 语句,当条件不再成立时,我们可以运行一次代码块:
i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/01.py
1
2
3
4
5
i is no longer less than 6
Process finished with exit code 0