Pass in Python
In this tutorial, you will learn about the pass statement in python, its syntax and how to use it in your python application.
Pass in Python
The pass statement in python is a null statement, i.e. the pass statement does nothing when executed. It is used as an error preventer for an empty code since empty code is invalid in loops, function definitions, class definitions, or in if statements.. It is used as a placeholder for future code.
Think of a scenario, where we have a function or loop that is not being implemented now, but in the future. We cannot re-write the whole code just for the inclusion of a function or loop in the future, but they also cannot contain an empty body. So to prevent this error, we can use the pass statement.
Syntax
pass
Now let us use the pass statement in some programs to understand it better.
Sample program using pass statement in python
Input
[gamipress_button type=”submit” label=”Run Code Snippet” onclick=”window.open(‘https://coderseditor.com/?id=1336′,’_blank’)”]
a=57 b=75 if a>b: print('A is greater than b') else: pass
Output:
Here, we can see that the value of ‘a’ is lesser than that of ‘b’, so the if condition becomes false and moves to the else part. In the else condition, instead of a return statement, we have given a pass. When you run the code, the program returns an empty line.
Sample program using pass statement in python
Input:
[gamipress_button type=”submit” label=”Run Code Snippet” onclick=”window.open(‘https://coderseditor.com/?id=1341′,’_blank’)”]
def pass_statement(): pass
Output:
Here, when you run an empty function it throws an error. So to prevent this error, we have used the pass statement.