The Python pass Statement

Introduction

The Python pass statement serves as a no-operation placeholder. It simply means “do nothing”. However, despite its lazy nature, it is useful as a placeholder for code that you are going to fill in later.

Because code blocks in Python are marked with indentations, Python statements that require code blocks underneath them need something to be indented, and the pass statement serves that purpose.

Basic pass Statement Syntax

The syntax for the pass statement is straightforward – it consists of the keyword pass on its own line. It can be used anywhere the Python interpreter expects a statement.

Here is the basic syntax for pass:

statement_that_requires_a_block:
    pass

Using the Python pass Statement

Suppose you are writing a function to greet dogs. You’ve decided you’re going to call the function greet_a_dog and it’s going to have a single parameter, dog_name. The function is ultimately going to print an appropriate greeting, but you are not sure how to implement it yet. In the meantime, you are going to work on some other code that uses that function, and in order to avoid errors, you’ll define the function with a body that consists of the pass statement only.

Example:

def greet_a_dog(dog_name):
    pass

The example above demonstrates using pass in place of the body of a function. The greet_a_dog function in the example does nothing and has no output, but it can be called from other code with no error, e.g. greet_a_dog('Snoopy').

After writing some other code, you thought of how you’d like the function to greet your favorite dog, Snoopy. But you still don’t know how it’s going to handle greeting other dogs. Consequently, you’ll write the function out, but put a pass placeholder for the code responsible for greeting other dogs.

Example:

def greet_a_dog(dog_name):
    if dog_name == "Snoopy":
        print("Hello, Snoopy, my favorite dog!!! What a good dog.")
    else:
        pass

greet_a_dog("Snoopy")
greet_a_dog("Astro")

Output:

Hello, Snoopy, my favorite dog!!! What a good dog.

The example above shows how to use pass in the else clause of an if statement. This new version of greet_a_dog() prints a greeting on line 3 if the dog is Snoopy but uses pass on line 5 to do nothing otherwise.

In addition to functions and else clauses, pass can also be used as a code-block placeholder with other constructs that require it.

With if statements

Example:

if dog == 'Astro':
    pass

With class definitions –

Example:

class Dog:
    pass

With for loops

Example:

for i in range(10):
    pass

With while loops

The pass statement can technically be applied to while loops, however, it’s of limited use because a while loop needs at least some code that will change its condition and terminate the loop, rendering the use of pass unnecessary.

Example:

command = ""
while command != "exit":
    command = input("Please enter your command: ")

Output:

Please enter your command: > sit
Please enter your command: > bark
Please enter your command: > shake
Please enter your command: > roll over
Please enter your command: > exit

The while loop in the example above doesn’t do much. However, it does change the loop condition (with user input in this case) to terminate the loop when the command is ‘exit’. Therefore, a pass statement is not needed because the loop does have a code block.

A while loop with just a pass for its body must have a false condition going in; otherwise, an infinite loop will result.

Example:

command = "exit"
while command != "exit":
    pass

In the example above, the pass statement is the only thing in the while loop, but the loop condition command != "exit" is false since command is assigned the value “exit” on line 1. Consequently, the loop does not iterate and there is no infinite loop.

Summary & Reference for the Python pass Statement

The Python pass statement serves as a no-operation placeholder. It is useful for code you are going to fill in later and requires an indented code block.


The syntax for the pass statement consists of the keyword pass on its own line.

def greet_a_dog(dog_name):
    pass