pass statement in Python with example
In this tutorial, you will learn how to use pass statement in Python programming language with example. The pass is a null operation/statement, nothing happens when it is executed. It results in no operation.
The pass is also useful in acting as a placeholder to indicate empty functions, classes, and loops.
Syntax of pass statement
pass
The main difference between pass and comment(statements starting with #) is that the interpreter ignores the comment entirely but not in the case of the pass statement. Now let us see the pass statement in Python with example.
Example of pass statement
#pass statement used for acting as a placeholder var1 ={'c','o','d','e','s','p','e','e','d','y'} for i in var1: pass
The output from the above piece of a snippet is that it does nothing i.e., performs an empty operation. The above code has a variable named var1 containing a list of characters. We have used a variable (i) iterating through the loop and performs nothing.
If we miss the pass, the code wouldn’t run. Results in an:
IndentationError: expected an indented block
If the pass statement not specified after the for loop body thereby it results in an Indentation error. Hence, it is a must so that there is a continuous flow of execution without any pause.
As pass statement is also useful for empty functions, classes, and loops. As the pass statement is also one of the loop control statements like the other two i.e., break and continue. Where all of them are having different functionality.
We can use the for the empty classes, functions as well as for the loops. Using more pass statement in Python with a suitable example.
# pass statement for class class Codespeedy: pass # pass statement for the function def speedy(args): pass
From the above code, we can see in a class we might not be ready to add to its entire content. Similarly in a method, we might not be ready to add its entire content. Hence, the pass statement is helpful.
Recommended blogs:
Leave a Reply