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?
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
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
What are break, continue and pass statements in Python? - Python - Data Science Dojo Discussions
Python: Explain the usage of the following functions - continue, break, pass
Difference Between Python pass and continue in Loops - TestMu AI Community
Explain the usage of the following functions - continue, break ...
Videos
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.