在本教程中,您将通过示例掌握 Python 中所有关于循环的知识;例如 for 循环和 while 循环,它们的语法和流程图,以及如何嵌套循环。此外,我们还将学习如何使用 break、continue 和 pass 等关键字来控制循环流程,以及如何在“for 循环”中使用 range 函数的示例。
迭代是针对多个实例重复一组代码或直到它们满足条件的过程。与迭代相关的两个重要因素是
迭代也称为循环。这是因为一系列指令会一个接一个地重复执行,直到满足条件,从而形成一个循环。
迭代大致分为两种类型。
考虑这样一个场景:您有一组代码,并且想要执行它,比如说六次。一种执行方法是编写这组代码 6 次并执行 6 次。但这会消耗大量的时间和空间。
所以这里有一种更简单的方法,即确定性迭代。在确定性迭代中,迭代次数在执行循环体之前是已知的。由于迭代次数是已知的,我们需要在循环开始时明确指定它。我们可以使用“For 循环”来说明确定性迭代的方法。
“for 循环”通常被称为“计数控制循环”,用于迭代字符串、列表、元组、集合和字典等序列。迭代次数取决于序列的长度。
For <var> in <iterables>:
<...........loop body…...>
其中
var 是在每次迭代中获取可迭代对象中值的变量。当变量到达序列的最后一项时,迭代停止,循环体通过缩进指定。
注意:在 Python 的 for 循环中不需要初始化索引变量。

以下示例显示了循环如何在列表和字符串上工作。
#List is iterable
L = ['Red','Green','Blue']
for i in L:
print(i)
#String is iterable
Str ='RGB'
for x in Str:
print(x)
#integer is not iterable
int = 8999
for i in int:
print(i)
输出
Red Green Blue R G B TypeError: 'int' object is not iterable
在上面的示例中,列表 L 和字符串 Str 是可迭代对象,i 是变量。在每次迭代中,变量 i 会获取 L 和 Str 中的连续项并打印结果。
像列表和字符串这样的顺序数据类型会执行并输出结果。另一方面,非顺序的整数类型对象会输出一个 TypeError,这意味着整数类型对象是不可迭代的。
注意:在 Python 中,所有属于集合或序列的数据类型都是可迭代的。
另一种但更具建设性的遍历序列的方法是使用索引。在 for 循环中进行索引并不像我们已经学过的字符串和列表那样简单。在深入了解索引之前,让我们先了解一下 len() 函数和 range() 函数。
len() 是 Python 中的一个内置函数,用于确定给定序列的长度。换句话说,len() 函数返回序列中项目的总数。
语法:
len(seq)
‘RGB’ 是一个长度为 3 的字符串,[‘Red’,’ Green’,’ Blue’] 是一个长度为 3 的列表。
range() 函数通常用于生成一个数字序列。
语法:
range( start,stop,step)
其中,start 表示序列的起始位置,默认为 0;stop 表示序列的结束位置,不包含在序列中;step 表示增量,默认为 1。
print(list(range(6)))
print(list(range(2,6)))
print(list(range(2,6,2)))
输出
[0, 1, 2, 3, 4, 5] [2, 3, 4, 5] [2, 4]
Range() 函数可以在 for 循环中使用,通过索引来迭代序列中的项目。下面的示例将澄清这个概念。
L = ['Red','Green','Blue','Black']
for i in range(1,len(L),2):
print(L[i])
输出
Green Black
不确定性迭代用于我们事先不知道执行次数的情况。在这里,代码的执行只要条件满足就会进行。While 循环属于不确定性迭代。
While 循环也称为条件控制循环,用于在满足条件的情况下迭代一组代码。如果条件不满足,则循环终止,控制权转移到外部语句。
while :
<body of while>

count = 0
while(count<5):
count=count+1
print(count)
输出
1 2 3 4 5
在此示例中:
注意:在使用 Python 循环时,请记住使用缩进。
在 Python 中,可以使用关键字“break”或“continue”在任何点中断循环。break 语句用于终止循环,而 continue 语句用于通过跳过循环中的某些代码来继续循环。
break 语句,顾名思义,在满足特定条件时会中断程序流程。换句话说,一旦满足给定条件,break 语句就会终止循环,并将控制权转移到循环外的下一个语句。
以下是循环中 break 语句的语法。
For <var> in <iterables>:
<...........loop body…...>
If <condition>:
break
While <condition 1>:
<body of while loop>
If <condition 2>:
break

L = ['Red','Green','Blue','Black','White']
for i in range(len(L)):
if L[i]=='Blue':
break
print(L[i])
print('Program Ends Here')
输出
Red Green Program Ends Here
count = 0
while(count<5):
count=count+1
if count==3:
break
print(count)
输出
1 2
continue 语句,正如其名,通过跳过满足所提供条件的某组代码来继续循环。Continue 语句从不像 break 语句那样终止循环。
以下是循环中 Continue 语句的语法
For <var> in <iterables>:
<...........loop body…...>
If <condition>:
continue
While <condition 1>:
<body of while loop>
If <condition 2>:
continue

L = ['Red','Green','Blue','Black','White']
for i in range(len(L)):
if L[i]=='Blue':
continue
print(L[i])
print('Program Ends Here')
输出
Red Green Black White Program Ends Here
count = 0
while(count<5):
count=count+1
if count==3:
continue
print(count)
输出
1 2 4 5
Python 允许的一个独特特性是在循环末尾使用可选的 else 块。下面给出了 for 循环和 while 循环中 else 块的语法。
For <var> in <iterables>
<...........loop body…...>
else:
<Statement(s)>
While <condition >
<body of while loop>
else:
<Statement(s)>
The
Str ='RGB'
for i in Str:
print(i)
else:
print('loop exits as string reaches its last character')
输出
R G B loop exits as string reaches its last character
count = 0
while(count<5):
count=count+1
print(count)
else:
print('Loop exits as count is no longer less than 5')
输出
1 2 3 4 5 Loop exits as count is no longer less than 5
Python 编程语言也允许在循环中使用嵌套原则。嵌套循环是包含另一个循环的循环。嵌套可以进行到任何级别,但通常嵌套到 2 或 3 层深度是方便的,因为级别越高,复杂性越高,也越容易出错。
For <var> in <iterables>:
For <var> in <iterables>:
<loop body>
<loop body>
While <condition 1>:
While <condition 2>:
<body of while loop>
<body of while loop>
在上面的语法中,我们看到了一个 for 循环在另一个 for 循环内部,以及一个 while 循环在另一个 while 循环内部。这个嵌套概念将在下面进行说明
L1 = ['Red','Green','Blue']
L2 = ['Car','Colour','Stone']
for i in L1:
for j in L2:
print(i,j)
print('\n')
输出
Red Car Red Colour Red Stone Green Car Green Colour Green Stone Blue Car Blue Colour
i=1
while(i<=3):
j=0
while(j<3):
j=j+1
print(i,'*',j,'=',i*j)
i=i+1
print('\n')
输出
1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 2 * 1 = 2 2 * 2 = 4 2 * 3 = 6
同样,我们可以在 while 循环中包含一个 for 循环,在 for 循环中包含一个 while 循环。但你需要记住的是关于缩进的使用。下面的例子展示了如何在一个 while 循环中嵌套一个 for 循环。
i=1
while(i<=3):
L2 = ['Car','Colour','Stone']
for j in L2:
print(i,j)
i=i+1
输出
1 Car 1 Colour 1 Stone 2 Car 2 Colour 2 Stone 3 Car
Python 中的 pass 语句是一个空语句,用于实现存根。pass 语句通常在 Python 程序中充当占位符。
Python 不允许循环为空。为了避免在这种情况下出现错误,我们使用 pass 语句。与注释不同,pass 语句不会被 Python 解释器忽略,相反,解释器会执行 pass 语句。pass 语句的结果将是无操作,因为执行 pass 语句时什么也不会发生。
Str ='RGB'
for i in Str:
pass