首页 > 编程知识 正文

python 怎么知道一个循环遍历完了,pythonfor循环语句基本用法及示例

时间:2023-05-04 23:11:59 阅读:266394 作者:2363

python循环语句for

In this tutorial you will learn about Python for & while loop, break & continue statement.

在本教程中,您将了解Python for&while循环,break和Continue语句。

Loops are a fundamental part for any computer program if decisions are to be made. Python provides us an excellent and easy to use looping constructs. Lets look at the following looping structures in Python.

如果要做出决定,循环是任何计算机程序的基本部分。 Python为我们提供了一个出色且易于使用的循环结构。 让我们来看一下Python中的以下循环结构。

Python while循环 (Python while Loop)

A while loop is used to execute a condition so long it gets evaluated to be true. It has following syntax.

while循环用于执行条件,只要条件被评估为真即可。 它具有以下语法。

Syntax

句法

while Condition:statement 1statement 2statement n

Image Source

图片来源

Example

a=5while a<10:print(a)a=a+1print("Out of Loop")

Output

输出量

We have initialized a variable ‘a’ to 5. Then we take a while construct and check for the condition if a is less than 10. For the first iteration, it is true and hence the statements within the loop executes and then value of a is incremented. On checking after the fifth iteration, the condition will be evaluated to false and control will come out of the loop. Hence, the print(“Out of Loop”) is executed.

我们已经将变量'a'初始化为5,然后进行了while构造并检查条件a是否小于10。对于第一次迭代,它为true,因此循环中的语句将执行,然后执行a的值增加。 在第五次迭代后进行检查时,条件将被评估为false,并且控制将退出循环。 因此,执行打印(“ Out of Loop”)。

Note that a=a+1 cannot be replaced by a++ in Python. You can also use a sentry variable in order to avoid the loop going into infinite mode. You need not use any braces or brackets in Python to define the loop structure. Proper indentations are prefect for Python interpreter to ascertain the scope of a loop.

请注意,在Python中a = a + 1不能用a ++代替。 您也可以使用哨兵变量,以避免循环进入无限模式。 您无需在Python中使用任何大括号或方括号来定义循环结构。 适当的缩进是Python解释器确定循环范围的完美手段。

Python for循环 (Python for Loop)

The for loop is used for repetition of a particular lines of codes in a program. Suppose you want to print numbers until 10, you can do it either by typing 10 print statements or by using a for loop. The for loop repeats a part of a program based on the sequence.

for循环用于重复程序中特定代码行。 假设您要打印直到10的数字,可以通过键入10个打印语句或使用for循环来完成。 for循环根据该序列重复程序的一部分。

for loop repeats its loop body for each of its elements in the given sequence. As soon as all the elements are executed, the loop terminates and the control comes out of its block.

for循环针对给定序列中的每个元素重复其循环主体。 一旦所有元素都被执行,循环就终止,并且控制从其块中出来。

Syntax

句法

for Counter in Variable:statement 1statement 2statement n

Image Source

图片来源

Example

a={1,2,3,4,5}for i in a:print i

Output

输出量

Here, a variable ‘a’ is defined with 5 values. The for loop here doesn’t check any condition as in case of a while loop. It just follows the sequence of a variable. We have also declared a counter variable ‘i’ which iterates throughout the loop. ‘in’ is a keyword used to mention the interpreter to loop through the variable ‘a’. Colon is necessary to tell the Python interpreter that the for loop block starts from next line onwards.

在此,变量“ a”定义为5个值。 这里的for循环不会检查任何条件,就像while循环一样。 它仅遵循变量的顺序。 我们还声明了一个计数器变量“ i”,该变量在整个循环中进行迭代。 “ in”是用于提及解释器以遍历变量“ a”的关键字。 冒号必须告诉Python解释器for循环块从下一行开始。

Note: Indentations are necessary as it helps the interpreter to identify the body of the for loop.

注意:缩进是必需的,因为它有助于解释程序识别for循环的主体。

Python range()函数 (Python range() Function)

There is also another method or a function that can be used with for loop. Python provides a pre-defined library function named range(). It automatically creates a sequence on its definition in a Python program. It provides us the facility to execute statements after some pre-defined iterations or skips. You can understand from the following example.

还有另一种方法或函数可以与for循环一起使用。 Python提供了一个名为range()的预定义库函数。 它会在Python程序中根据其定义自动创建一个序列。 它为我们提供了在一些预定义的迭代或跳过之后执行语句的功能。 您可以从以下示例中了解。

Example

for i in range(1,20,5):print i

Output

输出量

Here, range() is used to create a sequence starting from 1 and it ends at 20. But each time the value of counter variable ‘i’ will be increased by 5.

在这里,range()用于创建从1开始并以20结尾的序列。但是每次计数器变量'i'的值将增加5。

Python中断并继续声明 (Python break and continue Statement)

break and continue statements can be used in while and for loops. break statement terminates the loop execution and the control immediately come out of loop body. continue statement makes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

break和continue语句可以在while和for循环中使用。 break语句终止循环执行,并且控件立即退出循环主体。 Continue语句使循环跳过主体的其余部分,并在重新进行迭代之前立即重新测试其条件。

Image Source

图片来源

Image Source

图片来源

Example

a=5;while a<10:print(a)a=a+1if a>8:breakprint("Out of Loop")

Output

输出量

The above example demonstrates the use of a break statement in a while loop. Here, the value of a will be incremented until 8 and then the break statement will be executed and the control will come out of loop.

上面的示例演示了while循环中break语句的用法。 在这里,a的值将增加到8,然后执行break语句,并且控件将退出循环。

翻译自: https://www.thecrazyprogrammer.com/2015/08/python-for-while-loop-break-continue-statement.html

python循环语句for

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。