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

前端 未结 11 1332
孤街浪徒
孤街浪徒 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
    
    0 讨论(0)
  • 2020-11-27 09:53

    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.

    0 讨论(0)
  • 2020-11-27 09:55

    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
    
    0 讨论(0)
  • 2020-11-27 09:55
    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.

    0 讨论(0)
  • 2020-11-27 09:56

    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.

    0 讨论(0)
  • 2020-11-27 09:57

    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
    
    0 讨论(0)
提交回复
热议问题