Python pass Statement Example Code

By | April 25, 2020

Topic: Python pass Statement Example Code

The pass statement in Python With Example Programs

The Python pass statement indicates a null operation. Nothing happens, when the Python interpretor executes a pass statement. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed.

Python pass Statement Example Code

Python pass Statement Example Code

For example:

class MyClass: pass # a class with no methods (yet) , may be provided later


class MyClass:
    pass # empty statement, yet, no class features

Note: we can use pass statent on the same line or on next line as indented empty statement block.

In the above example, we declare an empty class named MyClass. Note that, a class definition may not be empty, we must provide at least a pass statement.

Why to use pass statement? Why not to use a Comment?

The main difference between a pass statement and a comment is that: In Python a pass statement is executable, although it asks the interpretor to do nothing. Weheras the comment is ignorable and is not executed. Hence we cannot use a comment in place of a pass statement in definition of an empty class.
For example, if we use a comment to indicate an empty class, there will be an error message as shown below:

class MyEmptyClass2:
    # empty class yet

Output: Syntax Error

What is the Puropse of Python pass statement?

The purpose of the Python pass statement is that pass represents an empty statement or null statement in Python code. We can use pass statement to indicate – “Do nothing”.

The pass statement in Python represents empty statement, empty functions, empty classes and empty loops.

Actually, we may use the pass statement if we are not prepared to give implimentation details for a function or class. therefore we will use pass statement for the time being and provide the complete implimentation details later.

Python pass Statement Example Code: a brief Tutorial

# Perfect Python Tutprial
# Use of pass statement example program

class MyEmptyClass:

# define an empty method - do nothing method

def my_function():

print("Python pass statement example Program: Do Nothing")

print("Program Started")

# create an object of empty class

obj1 = MyEmptyClass()

# call function that does nothing


print("Program Ended")

Python pass statement example Program: Do Nothing
Program Started
Program Ended

You will also Like Perfect Python Concepts:

Leave a Reply

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