Updated April 14, 2023
Introduction to pass Keyword in Python
A “pass” keyword refers to a null operation. Or in other words, one can say, “do nothing”. The question must have popped up in many reader’s minds “Then why do we use it?”. Python has the concept of keywords. Keywords are some reserved words in python that have special meaning with them and can’t be used as variable/function names. These keywords are case-sensitive. So, “pass” and “Pass” are two different entities for Python. So, while using keywords in python, keep in mind the cases. There are so many keywords that python supports like: break, if, else, pass, finally etc. Python supports looping conditions like for loop, while loop, nested loop. During these loops’ execution, there come many inevitable conditions where code has to pass through, but without doing anything. In this kind of situation “pass” keyword plays a pivotal role.
Syntax of pass:
pass
Examples of pass Keyword in Python
Given below are the examples of pass Keyword in Python:
Example #1
Code:
for numb in range(1,5):
if numb==2:
pass
else:
print ("Present Number = {} ".format(numb))
Output:
- As one can notice here, for number “2”, the pass statement has executed. This means it resulted in “No Operation”(NOP).
- A question might occur: Then this is what comment does. Isn’t it?
- True. Comment can be used. But interpreter ignores the commented section; however, the pass is not ignored. The interpreter goes through it and performs nothing.
Example #2
A class with two different methods.
Code:
class Animals:
def Noise(self):
print("Noise created")
def Silence(self):
pass
# Create class and call both methods.
b = Animals()
b.Silence()
b.Noise()
Output:
- One can see the class “Animals” has two kinds of functions: one which prints something while another function with the pass.
- The output will be looking quite convincible, as the “Silence” function doesn’t do anything, while the “Noise” function does(it prints).
Example #3
Code:
for letter in 'EDUCBA':
pass
print('Here is the last letter :', letter)
Output:
- As one can notice, till the time letter was there, for loop kept on passing it. For letters: “E”,”D”,”U”,”C”,”B”,”A” it passed for all.
- And the last letter was stored in the variable name “letter”, which was printed at the end.
Example #4
Code:
randomChoosenList = ['a', 0,2]
for item in randomChoosenList:
try:
print("The entry is", item)
r = 1/int(item)
break
except:
print("Oops!","Error occured.")
pass
print("The reciprocal of",item,"is",r)
Output:
- As one can notice, the pass keyword is used in exception handling here.
- In case 1, where the first item ‘a’ will throw an exception. It will be shown “Oops!”,”An error occured.” And then passed.
- In case 2 as well, a similar thing happens. Exception gets shown and then passed.
- However, in case 3, it passes well in a try block, which doesn’t make it go through the pass keyword.
- One can use the pass keyword in any of the blocks: try or except, depending on the requirement.
Example #5
One can also use the pass keyword for keeping empty function. It will be a function that will not do anything. This could be a temporary way of keeping functionality defined related to a function, which can be implemented later.
Here we can’t comment section and keep that section.
Code:
def Future(self):
#pass
Output:
- Because of the commented section, it has thrown an error.
- A question might come here: Can we have multiple pass statement in one function or logical block of python?
- The answer is Yes! That’s because the interpreter just goes through the pass keyword and does nothing. So, one can definitely have more than one.
Code:
class temp:
def Future(self):
pass
print("hi")
pass
print("hello")
b = temp()
b.Future()
Output:
- As one can see, here we have two pass keywords. And that’s just not doing anything.
Advantages of using pass Keyword in Python
- The keyword “pass” can be used as a placeholder by many coders. Later with the usage of code, the pass can be replaced by suitable logic. It can be proved savior for future tweaks in code.
- It helps programmers to come with skeleton program and logic syntactically by blocking space for empty function or empty code.
Code:
class Student_Database:
def Student_data_delete(self,i):
#implement this function later
pass
def Student_data_update(self,i):
#implement this function later
pass
def Student_data_delete(self,i):
#implement this function later
Pass
- Other scenarios could be that a skeleton program can be created and given to other parties for implementations.
Conclusion
Python is a great programming language that comprises of many keywords. “Pass” is one of the important keyword used widely while coding and specially handling the conditions there. Python doesn’t have the concept of ending line with a semicolon like in other languages. So the “pass” keyword helps in indicating the empty lines or the empty logics, which doesn’t have to do anything for now, but later point that can hold some logic. One who understood the concept of “pass” and examples here can further jump to other keywords of python to get a good grip over other important keywords in it.
Recommended Articles
We hope that this EDUCBA information on “pass Keyword in Python” was beneficial to you. You can view EDUCBA’s recommended articles for more information.