Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continue and pass: what's the difference?

Tags:

python

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.

like image 575
TheLastGIS Avatar asked Dec 21 '22 03:12

TheLastGIS


1 Answers

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.

like image 104
Benjamin Gruenbaum Avatar answered Apr 17 '23 07:04

Benjamin Gruenbaum