Python Pass Statement


In this section we will learn about the pass statement in Python. The pass statement is used as place holder for functions and loops and their implementation. When the pass statement is executed it does not affect the code or interpreter that is it does not have any function. The result of the pass statement is no operation or NOP.

The pass statement is considered to be as the null statement. The difference between a pass statement and a comment is that, comments are not run and are completely ignored by the interpreter.

The pass statement is used when we have defined a function or a loop but we do not want to implement that right now and we cannot leave the body of the function or loop empty hence we use pass statement.

If the body of the loop or function is left empty then error will be generated by interpreter, so pass statement is used to build a body of function or loop that will not affect the code.

 

Syntax of pass statement

The following is the syntax of the pass statement in Python programming language:

pass

Only a keyword is used for pass statement. This statement is usually used as place holder in Python programming language.

 

EXAMPLE of pass statement in Python

Consider the following example in which we defined a loop and used pass statement to implement the loop later.

CODE

>>> a = [1, 2, 3, 4, 5]

>>> for i in a:

          pass

In the above example, we defined a list to avoid error. The counter variable will traverse through the sequence or list.

Consider the following example in which we defined a class and a function that are left empty and used pass statement:

CODE

Defining a class

>>> class abc:

          pass

Defining the function

>>> def function(asd):

          pass