if pass and if continue in python

Shelly picture Shelly · May 9, 2016 · Viewed 66.4k times · Source

I saw someone posted the following answer to tell the difference between if x: pass and if x: continue.

>>> 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

What is the result for if not element when a = 0? Why when using continue, 0 is not printed?

Answer

Neo picture Neo · May 9, 2016

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