Python – pass Keyword

Created with Sketch.

Python – pass Keyword

The pass keyword as the name suggests does nothing. It is used as a dummy place holder whenever a syntactical requirement of a certain programming element is to be fulfilled without assigning any operation.
In other words, the pass statement is simply ignored by the Python interpreter and can be seen as a null statement. It is generally used as a dummy statement in a code block, for example in the if or else block.

Example: pass in for loop
for num in range(1,6):        
    if num==3:
        pass
    else:
        print ("Num =  {} ".format(num))

 

Output


Num = 1
Num = 2
Num = 4
Num = 5

The following is the definition of a function which will not do any operation when executed:

Example:
def myfunction():
    pass

 

The pass statement is commonly used in the early stages of program development to form a skeleton of the entire code.
Initially, the programmer will create place holders with no operation using pass for various conditional statements, functions, classes etc., and eventually will fill their functionality.

Leave a Reply

Your email address will not be published. Required fields are marked *