Pass Statement in Python – What Is It and How to Use It

Updated on July 27, 2024

Article Outline

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
*Image
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
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.
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.
The following program demonstrates the pass statement in the Python function.  
def my_function():

    pass
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.
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.

Updated on July 27, 2024

Link
left dot patternright dot pattern

Programs tailored for your success

Popular

Management

Data Science

Finance

Technology

Future Tech

Upskill with expert articles

View all
Hero Vired logo
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.
Blogs
Reviews
Events
In the News
About Us
Contact us
Learning Hub
18003093939     ·     hello@herovired.com     ·    Whatsapp
Privacy policy and Terms of use

|

Sitemap

© 2024 Hero Vired. All rights reserved