Using Pass Versus Continue in Python

A number of people have asked me about the discussion of the pass and continue clauses on page 140 of Beginning Programming with Python For Dummies. The example on that page is confusing a lot of people. Most people assume that when the example prints its output, the w should not appear as part of the output—as if the pass and continue clauses work precisely the same.

If you look at the second sentence of the first paragraph on page 140, you see that it tells you that pass and continue work almost the same way, except that the pass clause allows completion of the code in the if block in which it appears. This distinction is important. The continue clause is immediate, the pass clause isn’t. So, yes, you can achieve different results using pass or continue.

However, both clauses work in the same way in that they stop execution of the current loop and continue with the next loop. The difference is when they stop execution of the current loop and it all hinges on the if statement block.

Enough people have written about this particular example that I want to be sure there is no confusion about the difference between pass and continue. Please let me know if you have any additional questions about these two clauses at John@JohnMuellerBooks.com.