问题:python的for循环中的`continue`和`pass`之间有区别吗?

两个python关键字之间是否有任何显着差异continuepass如示例中所示

for element in some_list:
    if not element:
        pass

for element in some_list:
    if not element:
        continue

我应该知道吗?

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

for element in some_list:
    if not element:
        pass

and

for element in some_list:
    if not element:
        continue

I should be aware of?


回答 0

是的,他们做的事情完全不同。 pass只是不执行任何操作,而continue继续进行下一个循环迭代。在您的示例中,如果您在if:之后添加了另一个语句,则区别将变得显而易见pass。之后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

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.

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

回答 1

是,有一点不同。continue强制循环从下一次迭代开始,而pass意味着“此处没有代码要执行”,并将继续执行其余部分或循环体。

运行这些,看看有什么区别:

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

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.

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

回答 2

continue 将跳回到循环的顶部。 pass将继续处理。

如果pass在循环的末尾,则差异可以忽略不计,因为流量始终会回到循环的顶部。

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

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.


回答 3

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

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

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

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.

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

回答 4

它们之间是有区别的,
continue跳过循环的当前迭代并执行下一个迭代。
pass什么也没做。这是一个空语句占位符。
我想给你一个例子,这将更好地阐明这一点。

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

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

回答 5

是,有一点不同。Continue实际上跳过了循环的当前迭代的其余部分(返回到开头)。Pass是不执行任何操作的空白语句。

查看python文档

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.

See the python docs


回答 6

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

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


回答 7

在for循环中通过和继续之间的区别:

那么为什么要传入python呢?

如果要创建一个空的类,方法或块。

例子:

class MyException(Exception):
    pass


try:
   1/0
 except:
   pass

在上面的示例中如果没有“ pass”,将抛出IndentationError。

Difference between pass and continue in a for loop:

So why pass in python?

If you want to create a empty class, method or block.

Examples:

class MyException(Exception):
    pass


try:
   1/0
 except:
   pass

without ‘pass’ in the above examples will throw IndentationError.


回答 8

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

输出是

>>> This statement is from pass.

再次,让我们运行相同的代码进行较小的更改。

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

输出是-

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

通过不执行任何操作。计算不受影响。但是继续返回到循环的顶部进行下一步计算。

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

这样考虑:

通过: Python只能在缩进上工作!与其他语言不同,没有空的花括号。

因此,如果您不希望在条件为真的情况下不执行任何操作,则除了pass之外没有其他选择。

继续:仅在循环的情况下才有用。如果对于一定范围的值,您不想在该特定遍次的条件为真之后执行循环的其余语句,则必须使用continue。

Consider it this way:

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

So, if you want to do nothing in case a condition is true there is no option other than 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.


回答 10

pass当您需要一些空函数,类或循环以用于将来的实现,而无需执行任何代码时,可以在场景中使用它。
continue用于在循环中未满足某些条件且没有条件且您需要跳过当前迭代并移至下一个迭代的场景中使用。

pass could be used in scenarios when you need some empty functions, classes or loops for future implementations, and there’s no requirement of executing any code.
continue is used in scenarios when no when some condition has met within a loop and you need to skip the current iteration and move to the next one.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。