概述
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 for和While循环 for循环 (for loop) while循环 (while loop) 结论 (Conclusion)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复