Python pass statement – keyword to do nothing

In this Python article, we will discuss the pass statement and how it can be used by some examples.

1. What is pass statement in python?

pass is an in-built keyword that can be used as a placeholder. If we require a function or a loop without any code or inner body to execute and we wish to add to or implement it later then we may use a pass statement.

pass will act as a null operator which will do nothing when executed. But pass cannot be understood as a comment because the python interpreter does not ignore the pass keyword or statement which it does with the comments.

pass #Just the keyword

Some of the examples where we can use the pass statement are

  • We can use it as the body for an empty class
  • We can use it as the body of an empty function. When that function does nothing or is left for future implementation.
  • We can use it in for loop, while loop, if-else conditions, etc.

Further, let’s implement some examples using pass statements. This is important to remember that if we do not use pass statement then these examples will throw errors

1.1. pass statement for functions

Let’s implement an empty function using pass statement.

def check(arguments):

Let’s see how empty method behaves if we skip the pass statement.

>>> def check(arguments):
  File "<stdin>", line 2
IndentationError: expected an indented block

1.2. pass statement for class

Let’s implement an empty class using pass statement.

class Person:

Now let’s see how empty class behaves if we skip the pass statement.

>>> class Person:
  File "<stdin>", line 2
IndentationError: expected an indented block

1.3. pass statement in loops and conditions

Let’s implement a loop with an empty if block using pass statement.

list = [5, 10, 15, 20]
for item in list:
  if item == 10:
    pass # this helps define the empty if condition block
    print("Inside else", end=", ")

Note: Similar to functions and empty classes if skip the pass statement and have empty if block, it will not compile and will throw IndentationError: expected an indented block.

Note: In addition to pass we can also use the break and continue keywords to control the flow of loops in Python.

2. Conclusion

In this article, we have learned about the pass in python and how we can use it for empty class, function within loops, and conditional blocks.

Complete code samples are present onย Github project.

An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ๐Ÿ˜Š

Recommended -

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x