The ‘pass’ statement in Python is a simple yet powerful tool that allows you to handle scenarios where a statement is required syntactically, and you don’t want to execute the code.. The ‘pass’ statement acts as a placeholder, enabling you to maintain the structure of your code while you plan out or defer certain parts of your implementation.
In this article, we’ll explore the ‘pass’ statement, understand its usage, and look at some practical examples of pass statements.
What is pass Statement in Python?
The ‘pass’ statement in Python is a null operation. It does nothing when executed. It is used as a placeholder in Python, a language in situations where a statement is syntactically required but you don’t want to execute any code in Python language.
The pass statement can be used in various situations:
Classes and methods
Functions
Loops
conditional statements
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure
Syntax of pass in Python
Pass statement has very simple syntax. In any line or indentations where you want to skip the execution. Python developers can type the pass statement.
pass
Let’s understand the pass statement using the various situations in the Python language.
Using pass Statement in Empty Function
In this section, we will define the pass statement in the Python function. In this function, You don’t need to define the body of the function. Let’ see the example
Example
def myFunction(num:int):
pass
This function also similar like this function
def secondFunction(num:int):
return None
The following program demonstrates the whole program.
Program
def firstFunction(num:int):
pass
def secondFunction(num:int):
return None
x = firstFunction(2)
y = secondFunction(2)
print(x, type(x))
print(y, type(y))
Output
None <class 'NoneType'>
None <class 'NoneType'>
Using pass Statement in an Empty Class
We can also use the pass statement with Empty Class in Python language. It is very similar to a function. When you are defining a class but don’t have the details yet. Then you can use the ‘pass’ statement.
Example: Let’s see the error we get when we leave a class body empty.
class Apple:
# Apple Class Object
apple = Apple()
Output
C:UsersLenovoAppDataLocalProgramsPythonPython312python.exe: can't open file 'E:ProgrammingArticlesindex.p': [Errno 2] No such file or directory
PS E:ProgrammingArticles> python index.py
File "E:ProgrammingArticlesindex.py", line 5
apple = Apple()
^
IndentationError: expected an indented block after class definition on line 1
The interpreter was expecting some executable code within the class body. But it showed the error because the interpreter did not find anything in the class body.
Example: The following program corrects the previous error by passing the pass statement in the Python program.
Program
class Apple:
pass
apple = Apple()
print(apple)
print(type(apple))
Output
<__main__.Apple object at 0x000001B91156A030>
<class '__main__.Apple'>
Using pass Statement in Loop
We can also use the pass statement in the Python loop. We are also not allowed to leave an empty body in a loop. If we want to implement the loop later., or just leave it for someone else to implement. We need to write a pass statement in the loop’s body. Let us understand it through some examples of loops.
Example : The following program demonstrates the while loop with pass statement.
Program
i = 10
while i==10:
pass
print(10)
Output
There will be no output print.
The previous code will not print anything. Since there is a pass statement inside the while loop, The value of i is never changed. The condition i==1 will remain true. So the loop will be stuck in an infinite loop.
Syntax of for loop is also quite similar to what we discussed in the above code.
Example: The following program demonstrates the pass statement with a loop.
Program
a = [1,2,3,4,5,6,7,8,9]
for _ in a:
pass
print("We have reached the end of the program")
Output
We have reached the end of the program
Notice that in the previous program we did not enter an infinite loop. It is because the for loop iterates until the iterable which lists a in the program. It can be done using the iter() function as shown in code below.
Using pass Statement in Conditional Statement
In this section, we will use the pass statement in conditional statements (if,else and elif) to make the code more readable. In the skip conditional we have always choice to skip the conditional cases where our code would be doing nothing, I
Example: The following demonstrates the Conditional Statement in Python.
n = int(input("Enter the Digit = "))
print("n =", n)
if n<=10:
print("Hello")
elif n<=20:
pass
else:
print("World")
Output
Enter the Digit = 2
n = 2
Hello
Conclusion
In this article, we learned about the pass statement in Python. The pass statement is a null statement that can be used as a placeholder for future code. Using a ‘pass’ statement in the program, developers can plan and outline their code without causing runtime errors, enhancing readability and maintainability. While it should not replace the actual implementation of logic, ‘pass’ is invaluable during the initial stages of coding. That allows for a clear and organized workflow.
FAQs
What is the ‘pass’ statement in Python?
The ‘pass’ statement in a null operation that does nothing when executed. It is used as a placeholder in situations where a statement is syntactically required but no action is necessary.
When should I use the ‘pass’ statement?
You should use the pass statement in function definitions, class definitions, loops, and conditional statements when you need to create placeholders for code that you plan to implement later.
Can you give an example of using ‘pass’ in a function definition?
The following program demonstrates the pass statement in the Python function.
def my_function():
pass
Can ‘pass’ be overused?
The ‘pass’ is useful. It should not replace the actual implementation of logic. It is best used during the planning or initial stages of development to outline the structure of the code.
What is the difference between ‘pass’ and comments in Python?
The ‘pass’ is a statement that can be executed, whereas comments are ignored by the interpreter. ‘Pass’ can be used where a statement is syntactically required, while comments are used to explain the code.
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.