如果通过,如果在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
if pass and if continue in python
提问by Shelly
I saw someone posted the following answer to tell the difference between if x: pass
and if x: continue
.
我看到有人张贴了以下的答案告诉之间的区别if x: pass
和if 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 element
when a = 0
? Why when using continue
, 0 is not printed?
什么是结果if not element
的时候a = 0
?为什么使用时continue
不打印 0?
回答by Neo
Using continue
passes for the next iterationof the for loop
Using pass
just does nothing
So when using continue
the print
won't happen (because the code continued to next iteration)
And when using pass
it will just end the if
peacefully (doing nothing actually) and do the print
as well
使用continue
通行证的下一个迭代的for loop
使用pass
少了点什么
所以当使用continue
该print
不会发生(因为代码继续下一次迭代),
而且使用时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 element
will evaluate to true
when element = 0
. In python 0
is same as boolean false. In the first loop pass
does 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
将评估为true
when 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 element
will never be reached. Instead, the for loop will take the next value, 1
and start from the top.
这告诉 Python 停止这个 for 循环循环并跳到循环的下一个循环。所以print element
永远达不到。相反,for 循环将取下一个值,1
并从顶部开始。
回答by Vedang Mehta
There is a fundamental difference between pass
and continue
in Python. pass
simply does nothing, while continue
jumps to the next iteration of the for loop. The statement if not 0
always evaluates to True
, so both pass
and continue
statements will be executed. pass
will do nothing and print the value, while continue
will skip to the next iteration ignoring the print
statement written below.
Pythonpass
和continue
Python之间存在根本区别。pass
只是什么都不做,而continue
跳转到 for 循环的下一次迭代。该语句的if not 0
计算结果始终为True
,因此pass
和continue
语句都将被执行。pass
将什么都不做并打印值,而continue
将忽略print
下面写的语句跳到下一次迭代。
回答by Spade
continue
is 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 1
and 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 语句除了跳过并返回到下一行代码之外什么都不做。