How to use pass statement in Python

How to use pass statement in Python

2 mins read1.5K Views Comment
Updated on Nov 20, 2022 21:32 IST

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.

2022_08_Python-Pass-Statement.jpg

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.

Must Read: What is Python?

Must check: Python Online Course and Certification

Using pass Statement with the if condition.

Example

 
# declare list with integers
list = [2,-4,5,-8,3]
# iterate through the list
for item in list:
# if condition
if item<0:
# if True, do nothing
pass
# else condition
else:
# statement inside else
print( item,"is a positive number")
Copy code

Output

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

Example

 
# declare list with integers
list = [2,-4,5,-8,3]
# for loop
for item in list:
# do no operation while
# iterating through the
# list
pass
# print when for loop is finished
# iterating the list
print("End of for loop")
Copy code

Output:

End of for loop

Must Read: Pattern Program in Python

Must Read: Type Conversion in Python

Using pass Statement with Class

 
# an empty class A
class A():
pass
# an empty class B
class B():
pass
print("Both classes are valid.")
Copy code

Output:

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
# positive
def check_positive(number):
# if condition
if number<0:
# if True, do nothing
pass
# else condition
else:
# statement inside else
print( number,"is a positive number")
# function call
check_positive(1)
check_positive(-2)
check_positive(3)
check_positive(-4)
check_positive(5)
Copy code

Output

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:

2022_08_image-64.jpg

Must Read: Python if-else statement

Must Read: Abstraction in Python

Conclusion:

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.
About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio