In this Python guide, we’ll learn how to use Python pass statement with proper code examples. In order to give you a proper idea of how Python pass is used, we’ll be explaining these examples step by step.
After reading this post, you’ll have a complete idea of how to use Python pass statement in code.
So let’s not delay anymore and just get right into its implementation.
What is Python Pass?
It is actually a null statement that you can use within Python loops , if statements, functions and classes.
Suppose you want to specify an if statement but don’t want to pass anything as you would want to specify a code inside its body in the future. If you specify it without passing something to its body, then Python will give an error as the body cannot be empty.
So this is the reason that we use Python pass statement in it. Python does not ignore it but no action is performed by it as it is actually just a null statement.
Let’s now understand it using multiple practical examples. But first, let’s walk through its syntax.
Syntax of Pass Statement
It can be implemented using this simple pass keyword.
Implementing Python Pass Statement (Multiple Examples)
Below examples will satisfy the purpose of pass statement in Python code.
Example 1: Use Comment inside If Statement
Let’s pass a comment inside if statement and see if it works. See below code:
i=3 if(i==4): # will specify actions later
SyntaxError: unexpected EOF while parsing
In Python, comments are completely ignored which means nothing is actually passed to the body of if statement. As a result, we got a syntax error.
Example 2: Use Pass in If Statement
i=3 if(i==4): pass
By using Python pass statement in it, we won’t receive any syntax error and the process will execute successfully.
Reason is that the pass statement is not ignored by Python. It is executed but it does not perform any action so its good to use it, if you want to specify actions in future.
You can try using the Python pass statement in while loop, for loop, function and classes to see what outcome you get from it.
See below code for using it in for loop:
for var in sequence: pass
For function, see below code:
def functionName(args): pass
To use pass statement in class, see below code:
class exampleName: pass
So this is the role of Python pass statement in code. Hope you like this post.
Feel free to ask questions related to the implementation of pass statement in Python. I’ll be glad to answer all your questions.
As a conclusion of this post, hope you now have a clear practical knowledge of what Python pass is and how to properly use it in code. I’ll be very happy to have your valuable feedback on this post. Thank you for reading it.