Python syntax for an empty while loop

Question:

I have written this:

    while file.readline().startswith("#"):
        continue

But I suspect the continue is unnecessary? What is the correct syntax for what i’m trying to achieve?

Asked By: jsj

||

Answers:

while file.readline().startswith("#"):
    pass

This uses the pass statement :

The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action.

http://www.network-theory.co.uk/docs/pytut/passStatements.html

Answered By: ogzd

Accepted answer link to pass statement tutorial is no longer working see official docs here:

https://docs.python.org/3/tutorial/controlflow.html#pass-statements

pass is a null operation — when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed

Answered By: social
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.