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
Answer from Sven Marnach on Stack OverflowYes, 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
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
How does 'continue' and 'pass' work?
Python Break, Continue, Pass Statements with Examples
Difference Between Python pass and continue in Loops - TestMu AI Community
Python pass vs. continue - Stack Overflow
Videos
New to Python here, I started learning like 9 days ago and I am struggling to understand what the difference is between 'continue' and 'pass', don't know how to use them in loops, been searching YouTube videos and asking ChatGPT but I still couldn't get it. Anyone out there can help?
continue skips the statements after it, whereas pass do nothing like that. Actually pass do nothing at all, useful to handle some syntax error like:
if(somecondition): #no line after ":" will give you a syntax error
You can handle it by:
if(somecondition):
pass # Do nothing, simply jumps to next line
Demo:
while(True):
continue
print "You won't see this"
This will skip the print statement and print nothing.
while(True):
pass
print "You will see this"
This will keep printing You will see this
- "pass" just means "no operation". it does not do anything.
- "continue" breaks a loop and jumps to the next iteration of the loop
- "not 0" is True, so your "if not element" with element=0 triggers the continue instruction, and directly jumps to next iteration: element = 1
Using continue passes for the next iteration of the for loop
Using pass just does nothing
So when using continue the print won't happen (because the code continued to next iteration)
And when using pass it will just end the if peacefully (doing nothing actually) and do the print as well
'0' not printed because of the condition "if not element:"
If the element is None, False, empty string('') or 0 then , loop will continue with next iteration.
I'm an absolute beginner currently learning Python. I'm currently starting to learn loops, where these 3 functions appear. I'm having trouble understanding how the functions can be used in a code and I don't know the proper syntax. It'll really help me if you can provide a simple example that illustrates the 3 functions.