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

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


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... 012>>> for element in a:...     if not element:...         continue...     print element... 12


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 of the loop body.

Run these and see the difference:

for element in some_list:    if not element:        pass    print(1) # will print after passfor element in some_list:   if not element:       continue   print(1) # will not print after continue


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.