pass statement in python

Spread the love

The pass statement is used when a statement is required syntactically but no command or code has to be executed. It specifies a null operation or simply No operation (NOP) statement. Nothing happens when the pass statement is executed. Syntax of pass statement is simple, just type the keyword pass as shown below.


Let’s write a program to demonstrate pass statement

for letter in "PYTHON":
    pass # The statement is doing nothing
    print("Pass: ", letter)
Pass:  P
Pass:  Y
Pass:  T
Pass:  H
Pass:  O
Pass:  N

The pass statement is used as a placeholder. For example, if we have a loop that is not implemented yet, but we may wish to write some code in it in the future. In such cases pass statement can be written because we can not have an empty body of the loop. Though the pass statement will not do anything but it will make the program syntactically correct.

Difference between comment and pass statement –

The difference between a comment and pass statement is that while the interpreter ignores a comment entirely, pass is not ignored. Comment is not executed but pass statement is executed but nothing happens.

Difference between break, continue, and pass statement –

The break statement terminates the execution of the nearest enclosing loop in which it appears.

The continue statement skips the rest of the statements in the loop and transfers the control unconditionally to the loop-continuation portion of the nearest enclosing loop.

The pass statement is a do-nothing statement in a loop. It is just added to make the loop syntactically correct. That is , a pass statement is written as we cannot have an empty body of the loop.

Rating: 1 out of 5.

Leave a Reply