Is there a difference between "pass" and "continue" in a for loop in Python?
Asked Answered
G

13

380

Is there any significant difference between the two Python keywords continue and pass like in the examples

for element in some_list:
    if not element:
        pass

and

for element in some_list:
    if not element:
        continue

I should be aware of?

Galba answered 28/2, 2012 at 14:42 Comment(1)
@S.Lott: The example: while True:; pass # Busy-wait for keyboard interrupt (Ctrl+C) in the python docs confused me in the way, that I didn't find it clear weather it behaves equivalent to continue in this case or something else was intended. The first sentence "The pass statement does nothing." characterizes all the answers to my question, but somehow it didn't catch my eye.Galba
A
528

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)
... 
0
1
2
>>> for element in a:
...     if not element:
...         continue
...     print(element)
... 
1
2
Alatea answered 28/2, 2012 at 14:44 Comment(5)
i m totally agree with your answer. but i have still question regarding pass keyword is it needed ? and needed but why ? Thank YouPhototaxis
@HardikGajjar: Since the pass keyword doesn't do anything, it's only useful when you syntactically need an indented suite, but don't want to do anything. A common example is if you want to ignore some exception, you use except SomeException: pass. There are many other use cases as well. You wouldn't strictly need a keyword for this, since you could use any other statement that doesn't do anything (e.g. 0 is a perfectly valid statement that doesn't have an effect), but having a keyword for this allows you to be more explicit about not wanting to do anything.Alatea
@SvenMarnach is it frowned upon stylistically to treat exceptions with pass ?Unsupportable
@MikePalmice Not at all.Bank
'pass' keyword may also be used in skeleton codes someone may forward to a collaborator in order to avoid the error python interpreter shows in case you leave a branch statement (loops, conditionals, functions) empty so that the interpreter wouldn't interrupt and the collaborator may get the standard classes or function definitions which could be used by other collaborators simultaneously to work on an ope-source or any project which involves collaboration.Heavyset
H
114

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 pass

for element in some_list:
   if not element:
       continue
   print(1) # will not print after continue
Heighten answered 28/2, 2012 at 14:45 Comment(0)
C
38

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.

Chloro answered 28/2, 2012 at 14:44 Comment(0)
P
24

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.

Phenobarbital answered 3/11, 2019 at 7:14 Comment(0)
U
17

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.

>>> some_list = [0, 1, 2]
... 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
Urbai answered 30/4, 2016 at 7:34 Comment(0)
W
14

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
Whoosh answered 28/2, 2012 at 14:45 Comment(0)
P
8

Yes, there is a difference. Continue actually skips the rest of the current iteration of the loop (returning to the beginning). Pass is a blank statement that does nothing.

See the python docs

Plaudit answered 28/2, 2012 at 14:46 Comment(0)
H
3

In those examples, no. If the statement is not the very last in the loop then they have very different effects.

Huertas answered 28/2, 2012 at 14:44 Comment(0)
E
3
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.

Equitable answered 7/11, 2017 at 16:19 Comment(1)
What is the special difference with other answers already available?Sewel
N
3

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.

Notochord answered 29/3, 2018 at 6:0 Comment(2)
So, if you want to do nothing in case a condition is true there is no option other than pass --> this is not accurate, see e.g. Sven Marnach's comment above.Sundial
@Sundial Sorry, but he is correct. In the case where, for example, a variable can be both a float and an int, and you want to perform an operation on all ints, then you can simply use an if-statement that checks for floats, use a pass if the statement is True and perform the operation if it is False. This allows for efficient scanning of multi-purpose lists.Worriment
C
1

pass just continues the loop or the condition. It doesn't do anything. continue, though is used to skip the current iteration, and get to the next iteration.

You may ask, why is pass used at all if not needed? Consider the following case:

text = "I am a coder."

if text == "I am not a coder.":
    print("Programming is interesting! You should try it out!")
elif text == "I am a coder.":
    pass

pass is just a syntactical placeholder used to fill up some space. If you don't want to do anything if a particular condition checks out, you can use pass as a placeholder. You cannot just write an empty condition, loop or function in Python.

Convince answered 13/3, 2021 at 7:9 Comment(0)
A
1

pass just indicates emptiness doing nothing as shown below:

for i in range(3):
    if i == 1:
        pass
    print(i)

Output:

0
1
2

continue skips the current loop to the next loop as shown below:

for i in range(3):
    if i == 1:
        continue
    print(i)

Output:

0
2
Amelita answered 19/12, 2022 at 23:29 Comment(0)
C
0

pass could be used in scenarios when you need some empty functions, classes or loops for future implementations, and there's no requirement of executing any code.
continue is used in scenarios when no when some condition has met within a loop and you need to skip the current iteration and move to the next one.

Centaury answered 29/3, 2020 at 14:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.