What is the difference between continue
and pass
in Python? I am fairly new to Python and am trying to get my code looking and acting more professionally. I can see their value, but to my untrained mind, I can't see the clear difference. I have looked here but I couldn't really see what the main difference was. I noticed continue
is shown in a loop example to continue to the next loop and pass
is a 'place holder' in classes, etc.
I guess my question is, how necessary are they? Should I focus on them now to add professionalism to my code, or is it more of a take it or leave it scenario?
Thanks in advance for your response.
pass
means that you're just filling a place where a statement is usually needed
while True:
pass # The pass is needed syntactically
From the documentation:
pass is a null operation -- when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed, for example:
continue
goes to the next iteration if any.
i = 1
while i<5:
continue # Endless loop because we're going to the next iteration
i = i + 1
From the documentation:
continue may only occur syntactically nested in a for or while loop, but not nested in a function or class definition or finally statement within that loop.6.1It continues with the next cycle of the nearest enclosing loop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With