In the Python programming language, it is not possible to define a function or class, of statements or loops without writing the code. And doing so throws an error. Hence in cases where you want to have an empty code, and want to avoid the error being thrown, making use of a statement in Python called pass statement comes in handy. This serves as a placeholder for the code that you might write in the future. Whenever the pass statement is executed, no operation or NOP happens.
The Syntax to Define Pass Statement in Python Is as Follows:
def function_name():
pass
class class_name:
pass
if condition:
pass
for statement:
pass
Usage of Pass Statement in Empty Functions:
Sometimes whenever people work with big programs, there arises a necessity to include the ‘do nothing’ part for certain classes, loops, condition statements, etc. in order to not break the flow of the program.
The ‘do nothing’ part in the program cannot be empty in Python, as it results in an error because as per the convention in Python, no empty code is allowed.
The ‘do nothing’ part in Python can be handled using a statement called pass statement in Python.
By making use of the pass statement, the flow of the program continues by skipping the part where the pass statement is used, without throwing an error. The pass statement can be used in functions as well.
Example 1:
Create a Python program to demonstrate pass statements in functions using which the flow of the program is not affected. Also show how calling the function, including pass statement does not result in an error:
#defining a function consisting of pass statement to do nothing
def firstfunction():
pass
#defining the second function with no pass statement
def secondfunction():
print("This is the second statement after the function with pass statement is called")
#defining the main program
print("Welcome to Simplilearn, this is the first statement before the function with pass statement is called")
# calling the defined functions to display the output on the screen
firstfunction()
secondfunction()
print("Goodbye! That was demonstration of pass statement for you")
The output of the above program is shown in the snapshot below:
The above program defines a function whose body consists of a pass statement to do nothing. Then it defines the second function with no pass statement. Then it goes on and defines the main program. Then it goes on to calling the functions in order and demonstrating how the function with the pass statement is not executed and is simply skipped without affecting the flow of the program. The output is shown in the snapshot above.
Usage of Pass Statement in Empty Classes:
In some of the programs, you may have to include the ‘do nothing’ part in classes as well.
The ‘do nothing’ part in Python results in an error because Python does not allow empty code in the program.
In such cases to handle the ‘do nothing’ part in the program, you must make use of pass statements in Python in classes.
The pass statement in classes serves as empty code inside the classes without causing any error, however, objects of the empty classes can be created.
Example 2:
Python program to demonstrate pass statement in classes using which the flow of the program is not affected. It must also show how working with classes by creating objects including pass statement does not result in an error:
#defining an empty class using pass statement
class Organization:
pass
#creating an object of the empty class and displaying the details related to the object of the class as the output on the screen
object = Organization()
object.name = 'Simplilearn'
object.location = 'Bangalore'
print("\nThe details obtained by creating an object of the empty class are:")
print("Organization name:", object.name)
print("Organization location:", object.location)
The output of the above program is shown in the snapshot below:
The above program defines an empty class using a pass statement. It includes creating the object of an empty class and displaying the details related to the object of the class as the output on the screen. The output is shown in the snapshot above.
Usage of Pass Statement in Conditional Statements:
In some of the programs, there arises a necessity when you have to bypass the conditional statements though they are validated in the program.
In such cases, the ‘do nothing’ part must be included to bypass the conditional statements.
The ‘do nothing’ part in conditional statements can be handled by using pass statements in Python.
The conditional statements work like regular conditional statements despite the pass statement included.
Example 3:
Python program to demonstrate pass statement in conditional statements using which the flow of the program is not affected. And show how working with conditional statements including pass statement does not result in an error:
#defining two variables called firstvar and secodnvar consisting of integer values
firstvar = 100
secondvar = 20
#defining if condition statement to check which variable contains a lesser value and if the firstvar is lesser than the secondvar, then the output is just skipped using pass statement otherwise the print statement is executed to print the output
if (firstvar < secondvar):
pass
else:
print("the first condition statement validates to pass statement and this is the else part which states, the secondvar is less than firstvar")
The output of the above program is shown in the snapshot below:
The above program defines two variables called ‘firstvar’ and ‘secondvar’ to store the integer values. Then you must define the conditional statement and if statement to compare the two values. And if the first value is lesser than the second value, the output is just skipped using a pass statement, otherwise, the print statement is executed to print the output. The output is shown in the snapshot above.
Looking forward to making a move to the programming field? Take up the Python Training Course and begin your career as a professional Python programmer
Conclusion
In this tutorial, you explored the concept of pass statements in Python through programming examples and their outputs to demonstrate them. Simplilearn offers a Python Certification Course designed to help you learn everything in Python, to kick start your career. To top it off, this is a very flexible way to acquire skills in Python.
Do you have any questions for us regarding ‘Pass in Python’? Mention them in the comments section of this tutorial, and our experts will get back to you on the same, as soon as possible. Happy learning!