Python 简明教程
Python - Loops
Python Loops
Python 循环允许我们多次执行一条语句或一组语句。
Python loops allow us to execute a statement or group of statements multiple times.
通常,语句按顺序执行:函数中的第一条语句首先执行,其次是第二条,依此类推。有时,您可能需要多次执行代码块。
In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. There may be a situation when you need to execute a block of code several number of times.
编程语言提供了各种控制结构,允许执行更复杂的路径。
Programming languages provide various control structures that allow for more complicated execution paths.
Types of Loops in Python
Python 编程语言提供了以下类型的循环来处理循环需求 −
Python programming language provides following types of loops to handle looping requirements −
Sr.No. |
Loop Type & Description |
1 |
while loopRepeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body. |
2 |
for loopExecutes a sequence of statements multiple times and abbreviates the code that manages the loop variable. |
3 |
nested loopsYou can use one or more loop inside any another while, for or do..while loop. |
Python Loop Control Statements
循环控制语句改变了它在正常序列中的执行。当执行退出一个作用域时,在该作用域中创建的所有自动对象会被销毁。
Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.
Python 支持以下控制语句。点击以下链接查看详情。
Python supports the following control statements. Click the following links to check their detail.
让我们简单浏览一下循环控制语句
Let us go through the loop control statements briefly
Sr.No. |
Control Statement & Description |
1 |
break statementTerminates the loop statement and transfers execution to the statement immediately following the loop. |
2 |
continue statementCauses the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. |
3 |
pass statementThe pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute. |