Table of Contents
What does pass do in Python?
In Python pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action.
If you use a Python statement that expects a sub-block of code or suite, and one is not present, you will get a syntax error condition. For this reason, we have pass
, a statement that does absolutely nothing—it is a true NOP, to steal the “No OPeration” assembly code jargon. Style- and development-wise, pass
is also useful in places where your code will eventually go, but has not been written yet
Another use of pass
statement is as a place-holder for a function or conditional body when we are working on new code, so as to allow the developer keep thinking at a more abstract level. The pass
can also be used to create minimal classes. Hence, it can also be considered as a null statement. Hence, pass
statement helps avoiding the error when empty code is not allowed.
The pass
statement is widely used as a place-holder. When used with a condition, it behaves like a continue statement. The syntax of pass
statement when used with looping or a class is as shown below.
Some practical examples
Example 1 - Using pass in while loop
The example here iterates over the range of numbers from 1 to 10 and executes pass
as the developer does not know what to code inside the while loop. Hence, pass
act as a placeholder.
# Initializing
i=1
# Iterating while loop
while i <= 10:
# Developer doesn't know what to code inside the loop and so uses <code>pass
as a place-holder pass i+=1 else: print("Ending of while loop")
Output
Ending of while loop
Example 2 - Using pass while iterating over a list
When you want to iterate over the sequence one way is to iterate using the for loop given below that gives the position of the desired element. Here, we are performing linear search to search the element whose value is "China". The example below iterates over a Python list containing names of countries and skips the "China" while printing the other countries. Hence, it behaves like a continue statement.
# Initializing
l1=['India','Nepal','Srilanka','China','Australia']
for country in l1:
if country == 'China':
pass
else:
print (country)
Output
India
Nepal
Srilanka
Australia
Example 3 - Using pass statement with an empty function
In the example given below, we are writing a display function in Python containing only pass
statement as a place-holder. When we call this function, no action will be taken. Hence, it provides the developer a flexibility to structure the application and add the functionalities step by step.
# Initializing
def display():
pass
a=10
b=20
display()
print("value of a is "a, "and b is",b)
Output
value of a is 10 and b is 20
Example 4 - Using pass statement used with empty class
In the example given below, we are writing an empty python class. Python doesn't allow the empty class, but if we are not sure about the implementation of the class we can simply write pass
statement as a place-holder so as to add the implementation later during the development.
class employee:
pass
Summary
The knowledge of control flow statement pass
is core to python programming language that is very useful to formulate a complex logic easily and define the structure of the program before going for actual implementation. You will frequently need to use the pass
statements as a place holder to build varied of applications. The knowledge of pass
in different scenarios and combinations helps solving the really complex task to a time efficient solution. Hence, pass
statement helps avoiding the error when empty code is not allowed.
In this tutorial, we covered the pass
statement with for loop, while loop, empty function, empty class along with the difference between pass
and other control flow statements like break, continue and return with an example to demonstrate the functionalities of pass
statement. All in all, this tutorial, covers everything that you need to know in order to understand and use the pass
statement in Python.
References