Python Pass Statement

We will discover more about pass statements in this tutorial. It is interpreted as a placeholder for the future execution of functions, classes, loops, etc.

What is Pass Statement in Python?

The null statement is another name for the pass statement. A Comment is not ignored by the Python interpreter, whereas a pass statement is not. Hence, they two are different Python keywords.

We can use the pass statement as a placeholder when unsure what code to provide. So, we only have to place the pass on that line. Pass may be used when we don’t wish any code to be executed. We can simply insert a pass in places where empty code is prohibited, such as loops, functions, class definitions, or if-else statements.

Syntax of the Pass Keyword

Keyword:
pass

Typically, we utilise it as a reference for the future.

Let’s say we have a loop or an if-else statement that isn’t to be filled now but that we wish to in the future. The pass keyword cannot have an empty body as it will be syntactically wrong. An error would be displayed by the Python interpreter suggesting to fill the space. Therefore, we create a code block that performs nothing using the pass statement.

Example of the Pass Statement

Code

# Python program to show how to use a pass statement in a for loop
'''''pass acts as a placeholder. We can fill this place later on'''
sequence = {"Python""Pass""Statement""Placeholder"}
for value in sequence:
if value == "Pass":
pass # leaving an empty if block using the pass keyword
else:
print("Not reached pass keyword: ", value)

Output:

Not reached pass keyword:  Python
Not reached pass keyword:  Placeholder
Not reached pass keyword:  Statement

The same thing is also possible to create an empty function or a class.

Code

# Python program to show how to create an empty function and an empty class
# Empty function:
def empty():
pass
# Empty class
class Empty:
pass

Leave a Reply

Your email address will not be published. Required fields are marked *