我是靠谱客的博主 大意悟空,最近开发中收集的这篇文章主要介绍如何使用Python for和While循环 for循环 (for loop) while循环 (while loop) 结论 (Conclusion),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

编程,Python (Programming, Python)

Do you know Python for and while loops have else statements? Do you know how for and while loops work when used along with else, break, continue & try statements? Read further to understand more about this.

您知道Python forwhile循环具有else语句吗? 您是否知道forwhile循环与elsebreakcontinuetry语句一起使用时如何工作? 进一步阅读以了解更多信息。

for循环 (for loop)

The general syntax of for loop:

for循环的一般语法:

for val in sequence:
<<body of for loop>>

Let’s look at a couple of simple examples:

让我们看几个简单的例子:

for i in range(3):
print(i)
>> 0
>> 1
>> 2for c in "Hi":
print(c)
>> H
>> i

Now, let’s see how for loop works when else, break, continue, and try statements are used.

现在,让我们看看 使用 else break continue, try 语句 for 循环 如何 工作

  • else statement is executed only if the for loop has been terminated normally without encountering break statement.

    仅当for循环已正常终止而不遇到break语句时,才执行else语句。

  • When break statement is encountered, the control comes out of the for loop and execute next statement after the loop

    当遇到break语句时,控件从for循环中退出并在循环后执行下一条语句

  • when continue statement is encountered, it skips the rest of the code inside a loop for the current iteration and continues with next iteration.

    当遇到continue语句时,它将在循环中跳过当前迭代的其余代码,并继续进行下一个迭代。

  • finally statement always executes whether there is an exception or not.

    无论是否有异常, finally语句始终执行。

With this understanding of how else, break, continue and try statements work, let us look at the examples of for loop:

了解了elsebreakcontinuetry语句如何工作之后,让我们看一下for循环的示例:

Example 1: In this example, the break statement gets executed so control comes out of the for loop without executing else statement.

示例1 :在此示例中,执行了break语句,因此控制从for循环中出来而无需执行else语句。

image for post
Image by Author
图片作者

Example 2: In this example, since continue statement is used, all the statements are executed including the else statements.

示例2 :在此示例中,由于使用了continue语句,因此将执行所有语句,包括else语句。

image for post
Image by Author
图片作者

Example 3: In this example, since continue statement is used inside the except statement, all the statements are executed including the else statements. As you can notice, finally statement also gets executed irrespective of exception.

示例3 :在此示例中,由于在except语句内部使用了continue语句,因此将执行所有语句,包括else语句。 如您所见, finally语句也将与异常无关地执行。

image for post
Image by Author
图片作者

Example 4: In this example, ZeroDivisionError exception is caught. Since there is a break statement control comes out of the loop. But as expected finally statement is executed before coming out of the loop.

示例4:在此示例中,将捕获ZeroDivisionError异常。 由于存在一个break语句,因此控件退出了循环。 但是按预期, finally语句在退出循环之前已执行。

image for post
Image by Author
图片作者

while循环 (while loop)

The general syntax of while loop:

while循环的一般语法:

while test_expression:
<<body of while loop>>

Let’s look at a simple example:

让我们看一个简单的例子:

i = 0
while (i < 3):
print(i)
i+= 1
>> 0
>> 1
>> 2

Now, we can look at the examples for while loop when used with else, break, continue and try statements.

现在,我们可以看看与elsebreakcontinuetry语句一起使用时while循环的示例。

Example 1: In this example, since continue statement is used, all the statements are executed including the else statements. Notice that when i==2 the current iteration is skipped.

示例1 :在此示例中,由于使用了continue语句,因此将执行所有语句,包括else语句。 请注意,当i==2将跳过当前迭代。

image for post

Example 2: In this example, the break statement gets executed so control comes out of the while loop without executing else statement.

示例2 :在此示例中,执行了break语句,因此控制权从while循环中出来, while没有执行else语句。

image for post

Example 3: In this example, an exception is caught. Since there is a break statement control comes out of the while loop. But as expected finally statement is executed before coming out of the loop.

示例3:在此示例中,捕获了一个异常。 由于存在break语句,因此控制从while循环中出来。 但是按预期, finally语句在退出循环之前已执行。

image for post
Image by Author
图片作者

Example 4: In this example, since continue statement is used inside the except statement, all the statements are executed including the else statements. As you can notice, finally statement also gets executed irrespective of an exception.

示例4:在此示例中,由于在except语句内部使用了continue语句,因此将执行所有语句,包括else语句。 如您所见, finally语句也将与异常无关地执行。

image for post
Image by Author
图片作者

结论 (Conclusion)

Hope you have understood how for and while loop work with else, break, continue & try statements.

希望您了解forwhile循环如何与elsebreakcontinuetry语句一起使用。

Thank you so much for taking out time to read this article. You can reach me at https://www.linkedin.com/in/chetanambi/

非常感谢您抽出宝贵的时间阅读本文。 您可以通过https://www.linkedin.com/in/chetanambi/与我联系

翻译自: https://towardsdatascience.com/how-to-use-python-for-and-while-loops-6a6a3325929c

最后

以上就是大意悟空为你收集整理的如何使用Python for和While循环 for循环 (for loop) while循环 (while loop) 结论 (Conclusion)的全部内容,希望文章能够帮你解决如何使用Python for和While循环 for循环 (for loop) while循环 (while loop) 结论 (Conclusion)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部