4

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.

TheLastGIS
  • 426
  • 5
  • 18
  • 2
    Continue and pass do completely different things. The python official site has a great tutorial and reference for you to learn from. – Patashu Jun 26 '13 at 01:44
  • Nevermind... http://stackoverflow.com/questions/9483979/is-there-a-different-between-continue-and-pass-in-a-for-loop-in-python?rq=1 – TheLastGIS Jun 26 '13 at 01:44

2 Answers2

8

Pass

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

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.

Community
  • 1
  • 1
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
3

Pass is useful for creating functions with no use. It does absolutely nothing. I sometimes use it when starting a new project to create functions that I will use later, but I will have no need for them at the moment.

Continue, starts a loop again with the next element in the iteration, often found after a conditional.

TerryA
  • 58,805
  • 11
  • 114
  • 143