Do you know Python 您知道Python The general syntax of Let’s look at a couple of simple examples: 让我们看几个简单的例子: Now, let’s see how 现在,让我们看看 使用 仅当 When 当遇到 when 当遇到 无论是否有异常, With this understanding of how 了解了 Example 1: In this example, the 示例1 :在此示例中,执行了 Example 2: In this example, since 示例2 :在此示例中,由于使用了 Example 3: In this example, since 示例3 :在此示例中,由于在except语句内部使用了 Example 4: In this example, 示例4:在此示例中,将捕获 The general syntax of Let’s look at a simple example: 让我们看一个简单的例子: Now, we can look at the examples for while loop when used with 现在,我们可以看看与 Example 1: In this example, since 示例1 :在此示例中,由于使用了 Example 2: In this example, the 示例2 :在此示例中,执行了 Example 3: In this example, an exception is caught. Since there is a 示例3:在此示例中,捕获了一个异常。 由于存在 Example 4: In this example, since 示例4:在此示例中,由于在 Hope you have understood how 希望您了解 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 (Programming, 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.for和while循环具有else语句吗? 您是否知道for和while循环与else , break , continue和try语句一起使用时如何工作? 进一步阅读以了解更多信息。 for循环 (for loop)
for loop:for循环的一般语法: for val in sequence:
<<body of for loop>>for i in range(3):
print(i)
>> 0
>> 1
>> 2for c in "Hi":
print(c)
>> H
>> ifor 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语句。 break statement is encountered, the control comes out of the for loop and execute next statement after the loopbreak语句时,控件从for循环中退出并在循环后执行下一条语句 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语句始终执行。 else, break, continue and try statements work, let us look at the examples of for loop:else , break , continue和try语句如何工作之后,让我们看一下for循环的示例: break statement gets executed so control comes out of the for loop without executing else statement.break语句,因此控制从for循环中出来而无需执行else语句。
continue statement is used, all the statements are executed including the else statements.continue语句,因此将执行所有语句,包括else语句。
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.continue语句,因此将执行所有语句,包括else语句。 如您所见, finally语句也将与异常无关地执行。
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.ZeroDivisionError异常。 由于存在一个break语句,因此控件退出了循环。 但是按预期, finally语句在退出循环之前已执行。
while循环 (while loop)
while loop:while循环的一般语法: while test_expression:
<<body of while loop>>i = 0
while (i < 3):
print(i)
i+= 1
>> 0
>> 1
>> 2else, break, continue and try statements.else , break , continue和try语句一起使用时while循环的示例。 continue statement is used, all the statements are executed including the else statements. Notice that when i==2 the current iteration is skipped.continue语句,因此将执行所有语句,包括else语句。 请注意,当i==2将跳过当前迭代。
break statement gets executed so control comes out of the while loop without executing else statement.break语句,因此控制权从while循环中出来, while没有执行else语句。
break statement control comes out of the while loop. But as expected finally statement is executed before coming out of the loop.break语句,因此控制从while循环中出来。 但是按预期, finally语句在退出循环之前已执行。
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.except语句内部使用了continue语句,因此将执行所有语句,包括else语句。 如您所见, finally语句也将与异常无关地执行。
结论 (Conclusion)
for and while loop work with else, break, continue & try statements.for和while循环如何与else , break , continue和try语句一起使用。
最后
以上就是大意悟空最近收集整理的关于如何使用Python for和While循环 for循环 (for loop) while循环 (while loop) 结论 (Conclusion)的全部内容,更多相关如何使用Python内容请搜索靠谱客的其他文章。
发表评论 取消回复