Using Python pass keyword


Gaming Laptop Series

The Python pass keyword which is a statement in itself basically does nothing. It can be used as a placeholder for future code.

As empty code is not allowed in the loop’s body, Python conditional statements, functions definition, class definition, etc so by using the pass keyword you can avoid these errors.

In this article, we will discuss the usage of the pass keyword along with examples.

How to use pass keyword in Python

The syntax of using the pass keyword in Python is given below –

pass

Now see the following examples –

Example 1:

The following example shows how you can define a function without its definition in Python.

def sum_of_two():
            pass

Example 2:

You can define a loop with the empty body, look at the example below.

for i in range(0,5):
              pass

Example 3:

You can create an empty class in Python by using the pass keyword now see the example below.

class employee:
          pass

Example 4:

Similarly, you can use the pass in conditional statement –

for i in range(0,5):
             if i==2:
               pass
             else
               print(i)

Alternatives of using pass keyword in Python

Alternatively, you can use None, 0, True, or a string literal with text such as “This block does nothing” they all will perform nothing in the code. But for a better pythonic approach, these should be used in the right place in Python code.

Ok, that’s all for now. Now if you have a query then write us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.