PASS KEYWORD IN PYTHON | Python Pass

Python Pass

Now, we are going to have a look at another interesting and useful keyword, which is pass. We usually use the pass keyword as a placeholder for the future code in the loops or functions or etc… The thing is that when we write some function, or class, or loop, etc.. they cannot have an empty body. So, if we are writing these things in our program, but we do not have an idea that what are we going to write here, and we cannot even keep them empty, then we can just write pass there, which is just a placeholder for the future code.

We can say that since we cannot have the loops, functions, classes, etc. with an empty body, or we will just go into errors. So, we can just write the pass keyword, to specify that something will come soon here, but we do not have an idea, but we want to keep this function, loop, etc. in the code.

Do not confuse the pass keyword with comments. It is an entirely different thing. The comments are simply ignored, but the pass keyword is not ignored. We use the pass as the placeholder for the future code, when the empty code is not allowed, like in the loops, functions, class definitions, etc.

Python Pass

Let’s have a look at a simple example, which demonstrates the pass keyword.

As you can see, in the above program, we simply have a loop, where we are going to iterate from 0 to 9, since 10 is excluded. The thing is that the loop cannot be empty, and let’s say that right now we do not know that what are we going to do in this loop. So, in order to avoid errors, when empty code is not allowed, we are just writing the pass keyword. Just because we won’t get any error here, does not mean that we are done with the code. The pass keyword is used just as a placeholder for the future code.

Let’s now try to use the pass keyword with the if statement.

As you can see in the above code, we have a variable number, and then we are checking if the number is divisible by 2. But let’s say that at this moment, we do not know that what are we going to do, if the condition is True, and also we cannot keep the code as empty. So here, we just wrote the pass keyword, as the placeholder for some future code.

So, we can make use of the pass keyword, as and when required. Also, you can try to use this pass keyword with some other things, like function, or class definition. Remember that the pass keyword is used as a placeholder for some future code.