在python的for循环中,“continue”和“pass”之间有区别吗?

时间:2022-01-05 08:35:40

Is there any significant difference between the two python keywords continue and pass like in the examples

在示例中,这两个python关键字“继续”和“传递”之间有什么显著区别吗

for element in some_list:
    if not element:
        pass

and

for element in some_list:
    if not element:
        continue

I should be aware of?

我应该知道?

9 个解决方案

#1


233  

Yes, they do completely different things. pass simply does nothing, while continue goes on with the next loop iteration. In your example, the difference would become apparent if you added another statement after the if: After executing pass, this further statement would be executed. After continue, it wouldn't.

是的,他们做的事情完全不同。pass什么都不做,而continue继续进行下一个循环迭代。在您的示例中,如果您在if之后添加另一个语句:在执行pass之后,将执行进一步的语句,那么差异将变得明显。继续后,它不会。

>>> 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

#2


49  

Yes, there is a difference. continue forces the loop to start at the next iteration while pass means "there is no code to execute here" and will continue through the remainder or the loop body.

是的,有区别。继续强制循环从下一个迭代开始,而pass意味着“这里没有执行的代码”,并将继续通过其余的或循环体。

Run these and see the difference:

看看它们的区别:

for element in some_list:
    if not element:
        pass
    print 1 # will print after pass

for element in some_list:
   if not element:
       continue
   print 1 # will not print after continue

#3


7  

continue will jump back to the top of the loop. pass will continue processing.

continue将跳到循环的顶端。通过将继续处理。

if pass is at the end for the loop, the difference is negligible as the flow would just back to the top of the loop anyway.

如果pass在循环的末尾,那么差异是可以忽略的,因为流无论如何都会返回到循环的顶部。

#4


7  

In your example, there will be no difference, since both statements appear at the end of the loop. pass is simply a placeholder, in that it does nothing (it passes execution to the next statement). continue, on the other hand, has a definite purpose: it tells the loop to continue as if it had just restarted.

在您的示例中,没有区别,因为两个语句都出现在循环的末尾。pass只是一个占位符,因为它什么都不做(它将执行传递给下一个语句)。另一方面,continue有一个明确的目标:它告诉循环继续,就像它刚刚重启一样。

for element in some_list:
    if not element:
        pass
    print element  

is very different from

非常不同于

for element in some_list:
    if not element:
        continue
    print element

#5


4  

Yes, there is a difference. Continue actually skips the rest of the current iteration of the loop (returning to the beginning). Pass is a blank statement that does nothing.

是的,有区别。Continue实际上跳过循环当前迭代的其余部分(返回到开始)。Pass是一个什么都不做的空白语句。

See the python docs

看到python文档

#6


3  

In those examples, no. If the statement is not the very last in the loop then they have very different effects.

在这些例子中,没有。如果语句不是循环中的最后一个语句,那么它们有非常不同的效果。

#7


1  

There is a difference between them,
continue skips the loop's current iteration and executes the next iteration.
pass does nothing. It’s an empty statement placeholder.
I would rather give you an example, which will clarify this more better.

它们之间存在差异,继续跳过循环的当前迭代并执行下一个迭代。通过什么也不做。它是一个空语句占位符。我宁愿给你举个例子,这样能更清楚地说明这一点。

>>> for element in some_list:
...     if element == 1:
...         print "Pass executed"
...         pass
...     print element
... 
0
Pass executed
1
2

>>> for element in some_list:
...     if element == 1:
...         print "Continue executed"
...         continue
...     print element
... 
0
Continue executed
2

#8


0  

x = [1,2,3,4] 
for i in x:
    if i==2:
         pass  #Pass actually does nothing. It continues to execute statements below it.
         print "This statement is from pass."
for i in x:
    if i==2:
         continue #Continue gets back to top of the loop.And statements below continue are executed.
         print "This statement is from continue."

The output is

输出是

>>> This statement is from pass.

Again, let run same code with minor changes.

同样,让我们运行带有小更改的代码。

x = [1,2,3,4]
for i in x:
    if i==2:
       pass  #Pass actually does nothing. It continues to execute statements below it.
    print "This statement is from pass."
for i in x:
    if i==2:
        continue #Continue gets back to top of the loop.And statements below continue are executed.
    print "This statement is from continue."

The output is -

输出-

>>> This statement is from pass.
This statement is from pass.
This statement is from pass.
This statement is from pass.
This statement is from continue.
This statement is from continue.
This statement is from continue.

Pass doesn't do anything. Computation is unaffected. But continue gets back to top of the loop to procced with next computation.

通过不做任何事。计算未受影响。但是继续回到循环的顶端,进行下一个计算。

#9


-1  

Consider it this way:

这样考虑:

Pass: Python works purely on indentation! There are no empty curly braces, unlike other languages.

Pass: Python只在缩进上工作!不像其他语言,没有空的花括号。

So, if you want to do nothing in case a condition is true there is no option other than pass.

所以,如果你想在条件为真的情况下什么都不做,除了pass之外没有其他选择。

Continue: This is useful only in case of loops. In case, for a range of values, you don't want to execute the remaining statements of the loop after that condition is true for that particular pass, then you will have to use continue.

继续:这只对循环有用。如果对于一个值范围,您不希望在条件为该特定传递为true之后执行循环的其余语句,那么您必须使用continue。

#1


233  

Yes, they do completely different things. pass simply does nothing, while continue goes on with the next loop iteration. In your example, the difference would become apparent if you added another statement after the if: After executing pass, this further statement would be executed. After continue, it wouldn't.

是的,他们做的事情完全不同。pass什么都不做,而continue继续进行下一个循环迭代。在您的示例中,如果您在if之后添加另一个语句:在执行pass之后,将执行进一步的语句,那么差异将变得明显。继续后,它不会。

>>> 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

#2


49  

Yes, there is a difference. continue forces the loop to start at the next iteration while pass means "there is no code to execute here" and will continue through the remainder or the loop body.

是的,有区别。继续强制循环从下一个迭代开始,而pass意味着“这里没有执行的代码”,并将继续通过其余的或循环体。

Run these and see the difference:

看看它们的区别:

for element in some_list:
    if not element:
        pass
    print 1 # will print after pass

for element in some_list:
   if not element:
       continue
   print 1 # will not print after continue

#3


7  

continue will jump back to the top of the loop. pass will continue processing.

continue将跳到循环的顶端。通过将继续处理。

if pass is at the end for the loop, the difference is negligible as the flow would just back to the top of the loop anyway.

如果pass在循环的末尾,那么差异是可以忽略的,因为流无论如何都会返回到循环的顶部。

#4


7  

In your example, there will be no difference, since both statements appear at the end of the loop. pass is simply a placeholder, in that it does nothing (it passes execution to the next statement). continue, on the other hand, has a definite purpose: it tells the loop to continue as if it had just restarted.

在您的示例中,没有区别,因为两个语句都出现在循环的末尾。pass只是一个占位符,因为它什么都不做(它将执行传递给下一个语句)。另一方面,continue有一个明确的目标:它告诉循环继续,就像它刚刚重启一样。

for element in some_list:
    if not element:
        pass
    print element  

is very different from

非常不同于

for element in some_list:
    if not element:
        continue
    print element

#5


4  

Yes, there is a difference. Continue actually skips the rest of the current iteration of the loop (returning to the beginning). Pass is a blank statement that does nothing.

是的,有区别。Continue实际上跳过循环当前迭代的其余部分(返回到开始)。Pass是一个什么都不做的空白语句。

See the python docs

看到python文档

#6


3  

In those examples, no. If the statement is not the very last in the loop then they have very different effects.

在这些例子中,没有。如果语句不是循环中的最后一个语句,那么它们有非常不同的效果。

#7


1  

There is a difference between them,
continue skips the loop's current iteration and executes the next iteration.
pass does nothing. It’s an empty statement placeholder.
I would rather give you an example, which will clarify this more better.

它们之间存在差异,继续跳过循环的当前迭代并执行下一个迭代。通过什么也不做。它是一个空语句占位符。我宁愿给你举个例子,这样能更清楚地说明这一点。

>>> for element in some_list:
...     if element == 1:
...         print "Pass executed"
...         pass
...     print element
... 
0
Pass executed
1
2

>>> for element in some_list:
...     if element == 1:
...         print "Continue executed"
...         continue
...     print element
... 
0
Continue executed
2

#8


0  

x = [1,2,3,4] 
for i in x:
    if i==2:
         pass  #Pass actually does nothing. It continues to execute statements below it.
         print "This statement is from pass."
for i in x:
    if i==2:
         continue #Continue gets back to top of the loop.And statements below continue are executed.
         print "This statement is from continue."

The output is

输出是

>>> This statement is from pass.

Again, let run same code with minor changes.

同样,让我们运行带有小更改的代码。

x = [1,2,3,4]
for i in x:
    if i==2:
       pass  #Pass actually does nothing. It continues to execute statements below it.
    print "This statement is from pass."
for i in x:
    if i==2:
        continue #Continue gets back to top of the loop.And statements below continue are executed.
    print "This statement is from continue."

The output is -

输出-

>>> This statement is from pass.
This statement is from pass.
This statement is from pass.
This statement is from pass.
This statement is from continue.
This statement is from continue.
This statement is from continue.

Pass doesn't do anything. Computation is unaffected. But continue gets back to top of the loop to procced with next computation.

通过不做任何事。计算未受影响。但是继续回到循环的顶端,进行下一个计算。

#9


-1  

Consider it this way:

这样考虑:

Pass: Python works purely on indentation! There are no empty curly braces, unlike other languages.

Pass: Python只在缩进上工作!不像其他语言,没有空的花括号。

So, if you want to do nothing in case a condition is true there is no option other than pass.

所以,如果你想在条件为真的情况下什么都不做,除了pass之外没有其他选择。

Continue: This is useful only in case of loops. In case, for a range of values, you don't want to execute the remaining statements of the loop after that condition is true for that particular pass, then you will have to use continue.

继续:这只对循环有用。如果对于一个值范围,您不希望在条件为该特定传递为true之后执行循环的其余语句,那么您必须使用continue。