A pass statement in Python is a null statement that act as a placeholder for future code. In this article, we will briefly discuss how to use pass statement with the help of examples.
In this article, we are going to look at the Python Pass statement. So basically the past statement does absolutely nothing from a computational perspective. It is generally used syntactically inside a code block to perform no action. It acts as a placeholder for future code.
The pass statement in python is used with classes, loops, functions, and conditional statements to represent no operation. To better understand the concept of the pass statement in python, take look at the below examples.
# declare list with integerslist = [2,-4,5,-8,3]# iterate through the listfor item in list:# if conditionif item<0:# if True, do nothingpass# else conditionelse:# statement inside elseprint( item,"is a positive number")
2 is a positive number 5 is a positive number 3 is a positive number
As you can observe in the output of the above example, every time the if condition is True( ie, the element inside the list is negative), the code does nothing.
Must Read: Why use Python Datetime module?
Must Read: Free QR generator using Python
Using pass Statement with loops
# declare list with integerslist = [2,-4,5,-8,3]# for loopfor item in list:# do no operation while# iterating through the# listpass# print when for loop is finished# iterating the listprint("End of for loop")
# an empty class Aclass A():pass# an empty class Bclass B():passprint("Both classes are valid.")
Both classes are valid.
As you can observe in the above output, the print statement at the end gets executed without throwing up a syntax error. Currently, both the classes defined above are empty and the pass statement acts as a placeholder for future code.
Must Read: Comparison Operator in Python
Must Read: Memory Management in Python
Using pass Statement inside function definitions
# function check if a number is# positivedef check_positive(number):# if conditionif number<0:# if True, do nothingpass# else conditionelse:# statement inside elseprint( number,"is a positive number")# function callcheck_positive(1)check_positive(-2)check_positive(3)check_positive(-4)check_positive(5)
1 is a positive number 3 is a positive number 5 is a positive number
As you can observe in the output of the above code, no operation occurs in cases of -2 and -4.
Note: It is not possible to leave the statement inside a class, loop, or conditionals empty. That would raise a SyntaxError as shown below:
Must Read: Python if-else statement
Must Read: Abstraction in Python
We have managed to understand the basic functionality and use case of the pass statement in python. So we have explored the following key points:
- The pass statements state the interpreter to perform no operation in a code.
- The pass statements are used as a placeholder for future code.
- Without the pass statement, we will encounter Runtime errors like SyntaxError or IndentationError, as we can’t leave python statements empty.
Download this article as PDF to read offlineDownload as PDF