Is there a difference between `continue` and `pass` in a for loop in python?

前端 未结 11 1397
孤街浪徒
孤街浪徒 2020-11-27 09:05

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

for element in some_list         


        
11条回答
  •  没有蜡笔的小新
    2020-11-27 09:53

    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
    

提交回复
热议问题