Pass in Python
Introduction
In Python, a pass statement is a null statement but the difference between a comment and a pass statement is that comment is ignored while the interpretation and pass statement is not ignored.
Syntax
pass |
Python pass Example
Now let us consider a case where a user is writing some code and suddenly hit a line like in loops, conditions, functions etc, where the user does not what to write. So simply, the user will place a pass and will go on, because they can not be left empty. So we use the pass to avoid these types of errors.
Example 1
def function: |
In the above code, we used a pass statement in a function.
Example 2
class geekClass: |
In the above code, we used a pass statement in a class.
Example 3
n = 10 |
In the above code, we used a pass statement in a loop.
Example 4
x = 14 |
In the above code, we used a pass statement in an if-else conditional statement.
Example 5
l =[1,2,3,4,5] |
In the above code, we used a pass statement in a loop that traverses in a list.
In python, when not sure what code to write, so for placeholder, we can write a pass statement there. We only need to place the pass statement there. We use the pass when we do not wish any code to get executed. In places where empty code is not allowed, there we can place a pass statement.
Some more examples
Example 6
s = {"Python", "Pass", "Statement", "Placeholder"} |
Output
Not reached pass keyword: Python |
Example 7
class Node: |
Output
We left the node inside the Node class, so we can write it in future. |
Example 8
class Node: |
Output
In the above code, we used a pass statement inside the peek and push method, so we can use that space later to write their respective codes. |