Python Pass Statement

What is Python’s Pass Statement?

The pass statement is also known as the null statement. The Python mediator doesn’t overlook a Remark, though a pass proclamation isn’t. As a result, these two Python keywords are distinct.

We can use the pass statement as a placeholder when unsure of the code to provide. Therefore, the pass only needs to be placed on that line. The pass might be utilized when we wish no code to be executed. We can simply insert a pass in cases where empty code is prohibited, such as in loops, functions, class definitions, and if-else statements.

Syntax

Keyword:  

    pass

Ordinarily, we use it as a perspective for what’s to come.

Let’s say we have an if-else statement or loop that we want to fill in the future but cannot. An empty body for the pass keyword would be grammatically incorrect. A mistake would be shown by the Python translator proposing to occupy the space. As a result, we use the pass statement to create a code block that does nothing.

An Illustration 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

Comments

Leave a Reply

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