Iteration means executing the same block of code over and over, potentially many times. A programming structure that implements iteration is called a loop.
迭代意味着一次又一次地执行同一段代码。 实现迭代的编程结构称为循环 。
In programming, there are two types of iteration, indefinite and definite:
在编程中,有两种类型的迭代:不确定的和确定的:
-
With indefinite iteration, the number of times the loop is executed isn’t specified explicitly in advance. Rather, the designated block is executed repeatedly as long as some condition is met.
-
With definite iteration, the number of times the designated block will be executed is specified explicitly at the time the loop starts.
对于不确定的迭代 ,循环的执行次数没有事先明确指定。 相反,只要满足某些条件,就会重复执行指定的块。
通过确定的迭代 ,指定的块将在循环开始时明确执行的次数。
In this tutorial, you’ll:
在本教程中,您将:
- Learn about the
while
loop, the Python control structure used for indefinite iteration - See how to break out of a loop or loop iteration prematurely
- Explore infinite loops
- 了解
while
循环,这是用于不确定迭代的Python控制结构 - 了解如何提前退出循环或循环迭代
- 探索无限循环
When you’re finished, you should have a good grasp of how to use indefinite iteration in Python.
完成后,您应该掌握如何在Python中使用不确定的迭代。
Free Bonus: Click here to get our free Python Cheat Sheet that shows you the basics of Python 3, like working with data types, dictionaries, lists, and Python functions.
免费红利: 单击此处可获得我们的免费Python备忘单 ,其中显示了Python 3的基础知识,例如使用数据类型,字典,列表和Python函数。
while
循环 (The while
Loop)
Let’s see how Python’s while
statement is used to construct loops. We’ll start simple and embellish as we go.
让我们看看如何使用Python的while
语句构造循环。 我们将以简单而优美的方式开始。
The format of a rudimentary while
loop is shown below:
基本的while
循环的格式如下所示:
while while << exprexpr >> :
:
<< statementstatement (( ss )) >
>
<statement(s)>
represents the block to be repeatedly executed, often referred to as the body of the loop. This is denoted with indentation, just as in an if
statement.
<statement(s)>
表示要重复执行的块,通常称为循环的主体。 就像if
语句一样,用缩进表示。
Remember: All control structures in Python use indentation to define blocks. See the discussion on grouping statements in the previous tutorial to review.
切记: Python中的所有控件结构都使用缩进来定义块。 请参阅上一教程中有关分组语句的讨论。
The controlling expression, <expr>
, typically involves one or more variables that are initialized prior to starting the loop and then modified somewhere in the loop body.
控制表达式<expr>
通常涉及一个或多个变量,这些变量在启动循环之前已初始化,然后在循环主体中的某处进行了修改。
When a while
loop is encountered, <expr>
is first evaluated in Boolean context. If it is true, the loop body is executed. Then <expr>
is checked again, and if still true, the body is executed again. This continues until <expr>
becomes false, at which point program execution proceeds to the first statement beyond the loop body.
遇到while
循环时,首先在Boolean上下文中评估<expr>
。 如果为true,则执行循环体。 然后再次检查<expr>
,如果仍然为true,则再次执行主体。 这一直持续到<expr>
变为false,这时程序执行将继续执行循环体之外的第一条语句。
Consider this loop:
考虑以下循环:
>>> n = 5
>>> while n > 0 :
... n -= 1
... print ( n )
...
4
3
2
1
0
Here’s what’s happening in this example:
这是此示例中发生的事情:
-
n
is initially5
. The expression in thewhile
statement header on line 2 isn > 0
, which is true, so the loop body executes. Inside the loop body on line 3,n
is decremented by1
to4
, and then printed. -
When the body of the loop has finished, program execution returns to the top of the loop at line 2, and the expression is evaluated again. It is still true, so the body executes again, and
3
is printed. -
This continues until
n
becomes0
. At that point, when the expression is tested, it is false, and the loop terminates. Execution would resume at the first statement following the loop body, but there isn’t one in this case.
n
最初是5
。 第2行的while
语句标头中的表达式为n > 0
,这是正确的,因此将执行循环体。 在第3行的循环体内,n
递减1
到4
,然后打印。循环体完成后,程序执行将返回到循环的第二行,并再次对表达式进行求值。 这仍然是正确的,因此主体再次执行,并打印了
3
。这一直持续到
n
变为0
为止。 此时,当测试表达式时,它为false,并且循环终止。 执行将在循环体后的第一个语句处恢复,但在这种情况下没有一个。
Note that the controlling expression of the while
loop is tested first, before anything else happens. If it’s false to start with, the loop body will never be executed at all:
请注意,在发生其他任何事情之前,将首先测试while
循环的控制表达式。 如果开头是错误的,则循环体将永远不会执行:
>>> n = 0
>>> while n > 0 :
... n -= 1
... print ( n )
...
In the example above, when the loop is encountered, n
is 0
. The controlling expression n > 0
is already false, so the loop body never executes.
在上面的示例中,遇到循环时, n
为0
。 控制表达式n > 0
已经为假,因此循环体从不执行。
Here’s another while
loop involving a list, rather than a numeric comparison:
这是涉及列表而不是数字比较的另一个while
循环&