Python pass statement

Python pass

Python pass statements acts as a no operation. It can be used as a placeholder for implementation of a loop body, function body, or something like that, in the places where you need a statement, but does nothing at all.

When you compare it to other programming languages like Java, C or C++, you may notice that you can leave the body of a function, class or a loop in closed parenthesis. Following is a Java function with empty body.

public static void printMessage() {

}
Run

But in Python, a loop, function or a class cannot have its body empty. Following is not valid in Python.

def printMessage():
    #nothing

So, pass statement comes to help. You can have an empty function, that does nothing, with pass statement as only statement for the function body.

def printMessage():
    pass
Run

Python Pass statement in For Loop

Following example demonstrates the usage of pass statement to define an empty for loop, that does nothing.

for x in range(10) :
    pass
Run

Python Pass statement in Class

Following example demonstrates the usage of pass statement to define an empty for loop, that does nothing.

class Student:
    pass
Run

Python pass vs Python comment

The main difference between pass statement and comments is in terms of how Python Interpreter considers these statements.

Python Interpreter ignores a comment for execution, but does not ignore a pass statement. Python Interpreter executes pass statement just like any other statement, but the catch is that pass does nothing.

So, you can consider that Python pass statement is a dummy statement, which does nothing, but can fill for a Python statement when needed.

Python pass vs Python continue statements

The difference between pass and continue statements is based on the jumping of program execution after its execution.

pass statement passes the execution to next statement.

continue statement passes the execution to next iteration of loop.

Summary

In this Python Tutorial, we learned what pass statement is, and how we can use it for filling in empty loop, function or class bodies.