如果通过,如果在python中继续

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37124717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:53:46  来源:igfitidea点击:

if pass and if continue in python

pythonif-statementcontinue

提问by Shelly

I saw someone posted the following answer to tell the difference between if x: passand if x: continue.

我看到有人张贴了以下的答案告诉之间的区别if x: passif x: continue

>>> a = [0, 1, 2]
>>> for element in a:
...     if not element:
...         pass
...     print(element)
... 
0
1
2
>>> for element in a:
...     if not element:
...         continue
...     print(element)
... 
1
2

What is the result for if not elementwhen a = 0? Why when using continue, 0 is not printed?

什么是结果if not element的时候a = 0?为什么使用时continue不打印 0?

回答by Neo

Using continuepasses for the next iterationof the for loop
Using passjust does nothing
So when using continuethe printwon't happen (because the code continued to next iteration)
And when using passit will just end the ifpeacefully (doing nothing actually) and do the printas well

使用continue通行证的下一个迭代for loop
使用pass少了点什么
所以当使用continueprint不会发生(因为代码继续下一次迭代),
而且使用时pass它会刚刚结束的if和平(什么都不做实际上),然后执行print,以及

回答by Anoop

'0' not printed because of the condition "if not element:"

由于条件“如果不是元素:”而未打印“0

If the element is None, False, empty string('') or 0 then , loop will continue with next iteration.

如果元素是 None, False, empty string('') or 0 then ,循环将继续下一次迭代。

回答by WreckeR

From: https://docs.python.org/2/tutorial/controlflow.html#pass-statements

来自:https: //docs.python.org/2/tutorial/controlflow.html#pass-statements

The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action.

pass 语句什么都不做。它可以在语法上需要语句但程序不需要操作时使用。

In your code snippet above if not elementwill evaluate to truewhen element = 0. In python 0is same as boolean false. In the first loop passdoes nothing, so it prints all three elements. In the second loop, continue will stop the execution of the rest of loop for that iteration. so the print statement never executes. so it only prints 1 and 2.

在上面的代码片段中,if not element将评估为truewhen element = 0。在python0中与boolean false相同。在第一个循环pass中什么都不做,所以它打印了所有三个元素。在第二个循环中, continue 将停止执行该迭代的其余循环。所以打印语句永远不会执行。所以它只打印 1 和 2。

回答by C14L

if not element:

In both examples, this will only match the 0.

在这两个示例中,这只会匹配0.

pass

This does nothing. So the next command, print element, will be executed.

这没有任何作用。因此print element,将执行下一个命令。

continue

This tells Python to stop this for loop cycle and skip to the next cycle of the loop. So print elementwill never be reached. Instead, the for loop will take the next value, 1and start from the top.

这告诉 Python 停止这个 for 循环循环并跳到循环的下一个循环。所以print element永远达不到。相反,for 循环将取下一个值,1并从顶部开始。

回答by Vedang Mehta

There is a fundamental difference between passand continuein Python. passsimply does nothing, while continuejumps to the next iteration of the for loop. The statement if not 0always evaluates to True, so both passand continuestatements will be executed. passwill do nothing and print the value, while continuewill skip to the next iteration ignoring the printstatement written below.

PythonpasscontinuePython之间存在根本区别。pass只是什么都不做,而continue跳转到 for 循环的下一次迭代。该语句的if not 0计算结果始终为True,因此passcontinue语句都将被执行。pass将什么都不做并打印值,而continue将忽略print下面写的语句跳到下一次迭代。

回答by Spade

continueis a control-flow statement used to escape the innermost body of iteration. When your code hits

continue是用于转义最内层迭代体的控制流语句。当您的代码命中时

if not element

the interpreter skips for all values of element that don't validate totrue. 0 is one such value and it skips to the next iteration of the loop when it does not encounter the continue statement and therefore goes on to print the value of element 1and thereafter 2

解释器会跳过所有未验证为 的元素值true。0 是一个这样的值,当它没有遇到 continue 语句时它会跳到循环的下一次迭代,因此继续打印元素的值,1然后再打印2

In contrast, the pass statement does nothing but skip and return to the next line of code to execute.

相比之下,pass 语句除了跳过并返回到下一行代码之外什么都不做。