In this tutorial, we will discuss the python pass keyword or statement and, when and where we can use pass. We will see examples of pass keyword and understand how it works.
Contents of Tutorial
What is Pass in Python?
In Python, the pass statement is considered as no operation statement, means it consumes the execution cycle like a valid python statement but nothing happens actually when the pass is executed.
The pass statement is much like a comment, but the python interpreter executes the pass statements like a valid python statement, while it ignores the comment statement completely.
It is generally used to indicate “null” or unimplemented functions and loops body.
What are the uses of pass statements and How it Works
- Pass work like when the condition is true it goes in the pass and executes code of pass block after that it continues with the main program code.
- It can be used when a statement is required syntactically but the program requires no action.
- In python, if you use an if, elif, else, functions, class, for loops, while loops you need to define their body or block of code correspond to them, if you don’t the python interpreter will throw an error. So, to overcome this error you can use the pass keyword as a body of the corresponding statement and the statement do nothing and throw no error.
Syntax of pass Statement
It has a very simple syntax,
We can’t really leave the future implementation of methods or logic empty since this would throw an error when executed. Because of this, the
pass statement comes in handy.
Let’s Understand pass Keyword With the Help of Example
1. What if we do not use the pass keyword?
number = 20 if number == 19: else: print("this is else block")
File "c:/Users/PythonPool/Desktop/test.py", line 3 else: ^ IndentationError: expected an indented block
What happened in the code above or Behind the code
In the above python program, We haven’t declared the body of if statement. So when the python interpreter reaches at the if statement. Python interpreter didn’t find any code for if block.
Even though its expression was wrong, it did not goanna run in either way but it should have a body. At the moment interpreter finds that the if statement does not have a body correspond to it the interpreter threw an error.
2. Using the pass keyword/statement and solving the error
number = 20 if number == 19: pass else: print("this is else block")
this is else block
Note: So, to avoid compilation errors, you can use the
Pass Statement for an Empty Function
Python doesn’t have the concept of abstract functions. So if we have to define an empty function, we can’t write it like this.
IndentationError: expected an indented block
We can use the pass statement to define an empty function. The function will have a statement but it won’t do anything.
def foo(): pass
Can We Use Multiple pass Statements in Python Function?
Yes, we can have multiple pass statements in a function or a code block in Python.
It’s because the pass statement doesn’t terminate the function. Its only work is to provide an empty statement.
def myfun(): pass print('myfun') pass if True: pass pass print('True') else: print('False') pass pass
Python Pass statement with exception handling
If you don’t want to do anything for a specific type of Exception after catching it you can use pass statement.
numbers = [89, 102, 0, 234, 67, 10, 333, 32] for num in numbers: try: result = 1000/num print('Result is',result) except ZeroDivisionError: print('Division by Zero') result = 0 except AttributeError: pass
Result is 11.235955056179776 Result is 9.803921568627452 Division by Zero Result is 4.273504273504273 Result is 14.925373134328359 Result is 100.0 Result is 3.003003003003003 Result is 31.25
Examples of Python Pass Statement
Example 1: Python program that uses class with a pass
class Box: pass class Bird: def chirp(self): print("Bird chirped") def fly(self): pass x = Box() b = Bird() b.fly() print("Z")
Example 2: Python program that uses pass statement in for loop
for letter in 'Python': if letter == 'h': pass print 'This is pass block' print 'Current Letter :', letter print "End!"
Current Letter : P Current Letter : y Current Letter : t This is pass block Current Letter : h Current Letter : o Current Letter : n End!
Example 3: Using the pass keyword in an if statement
a = 33 b = 200 if b > a: pass
Example 4: Python program that uses pass in a while loop
import random def x(): result = random.randint(0, 5) print(result) return result while x() >= 1: pass print("END")
1 5 5 0 END
Pass statement vs comment
You may be wondering does python comment works similar to the pass statement as it does nothing so we can use the comment in place of pass statement. Well, it is not the case, a comment is not a placeholder and it is completely ignored by the Python interpreter while on the other hand pass is not ignored by the interpreter, it says the interpreter to do nothing.
Can we use comment instead of pass?
We can not use comment instead of the pass because the interpreter will ignore the comment and pass statement returns null.
Pass is a null operation—when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed.
So in this post, we saw how a pass statement can be used inside a Python program, without disturbing other parts of code and letting the Python interpreter execute the code which hasn’t been implemented as well.
In addition, if you have any doubt or query comment down below. We will try to solve it asap.