Python pass Statement

In this tutorial, you will learn about the pass statement in Python with the help of examples.

The pass statement is a null statement that is used as a placeholder for future implementation of functions, loops, etc.

In Python, the difference between pass and comment is that the interpreter ignores the comment, it doesn't ignore the pass statement.

Sometimes, pass is used when the user doesn't want any code to execute. So users can simply place pass where empty code is not allowed, like in loops, function definitions, etc., avoiding the possible error.

Syntax of pass

The syntax of the pass statement is very straightforward:

pass

Example of Python pass Statement

Suppose you need to create a loop or a decision-making statement. However, we are not sure yet what its body will be.

Let's try doing something like this:

number = 5.5

if number > 0.0:
    # implement this later

Output

File "<string>", line 4
    # implement this later
                         ^
SyntaxError: unexpected EOF while parsing

This results in an error message. It is because the body of the if statement is empty; comments don't count because Python completely ignores comments.

In such scenarios, we can use the pass statement

number = 5.5
if number > 0.0:
    pass

This code will run without any errors.

Use of Python pass Statement

Let's see some scenarios where we can use the pass statement.

1. pass in empty function

def function(argument1, argument2):
    pass

Here, the function body consists of a pass statement, which means that no code or action is executed within the function.

This indicates that the function is expected to be implemented or filled in with the actual logic at a later stage.

2. pass in empty class

class Python:
    pass

By using the pass statement as the class body, it serves as a placeholder to satisfy the syntax requirement of defining a class.

It allows the class to be defined without specifying any specific attributes or methods at that moment.

3. pass with if statement

if False:
    pass
else:
    print('Else Block')

In this code, the if statement checks if the condition False is true. However, since False is a boolean value that evaluates to False, the condition is not satisfied, and the code inside the if block is not executed.

Here the pass statement ensures that the code is syntactically correct and satisfies the requirement of having a statement inside the block.

Recommended Reading: Python if…else statement

Did you find this article helpful?