Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between pass and continue in python [duplicate]

My test shows that both pass and continue can be used equivalently to construct a empty for-loop for test purpose. Are there any difference between them?

like image 262
user3054997 Avatar asked Dec 04 '13 05:12

user3054997


People also ask

What is the difference between pass and continue Python?

Difference between pass and continue continue forces the loop to start at the next iteration whereas pass means "there is no code to execute here" and will continue through the remainder of the loop body.

What is the difference between pass and continue statement?

The pass statement is used as a placeholder for future code. When the pass statement is executed, nothing happens, but you avoid getting an error when empty code is not allowed. Empty code is not allowed in loops, function definitions, class definitions, or in if statements.

What is the difference between pass and return in Python?

Return exits the current function or method. Pass is a null operation and allows execution to continue at the next statement.

What does pass do in Python loop?

The pass statement tells the program to disregard that condition and continue to run the program as usual.


4 Answers

The pass keyword is a "no-operation" keyword. It does exactly nothing. It's often used as a placeholder for code which will be added later:

if response == "yes":
    pass  # add "yes" code later.

The continue keyword, on the other hand, is used to restart a loop at the control point, such as with:

for i in range(10):
    if i % 2 == 0:
        continue
    print(i)

That loop will only output the odd numbers since continue returns to the loop control statement (for) for iterations where i is even.

Contrast that with the exact same code, but using pass in place of continue:

for i in range(10):
    if i % 2 == 0:
        pass
    print(i)

That loop prints all the numbers in the range, since pass does not return to the loop control statement for even (or any) values of i. It simply drops through to the print statement.

In terms of an empty for loop, you're correct that they're functionally identical. You can use either of:

for i in range(10):
    pass
for i in range(10):
    continue
like image 164
paxdiablo Avatar answered Sep 24 '22 02:09

paxdiablo


pass does nothing (no operation), while continue make control flow to continue to next cycle of the loop.

like image 40
falsetru Avatar answered Sep 25 '22 02:09

falsetru


If the loop contains only a single statement, a pass or continue won't make any difference. But if there are multiple statements, then it matters:

for item in my_list:
    pass
    print(item) #This will be executed

for item in my_list:
    continue
    print(item) #won't be executed

Basically, the pass statement do nothing, while the continue statement will restart the loop.

But in your case:

for item in my_list:
    pass
    #Since there's nothing after pass, the loop is finished.
for item in my_list:
    continue
    #You're restarting the loop

There difference is not very visible.

Hope this helps!

like image 44
aIKid Avatar answered Sep 26 '22 02:09

aIKid


continue means "skip to the end of the loop body". If it's a while loop, the loop continues on to the loop test; if it's a for loop, the loop continues on to the next element of whatever it's iterating over.

pass does absolutely nothing. It exists because you have to have something in the body of an empty block statement, and pass is more readable than executing 1 or None as a statement for that purpose.

like image 39
user2357112 supports Monica Avatar answered Sep 22 '22 02:09

user2357112 supports Monica