Python pass Statement (original) (raw)

Last Updated : 04 Jan, 2025

Pass statement in Python is a null operation or a placeholder. It is used when a statement is syntactically required but we don’t want to execute any code. It does nothing but allows us to maintain the structure of our program.

**Example Use of Pass Keyword in a Function

Pass keyword in a function is used when we define a function but don’t want to implement its logic immediately. It allows the function to be syntactically valid, even though it doesn’t perform any actions yet.

Python `

Example of using pass in an empty function

def fun(): pass # Placeholder, no functionality yet

Call the function

fun()

`

**Explanation:

Let’s explore other methods how we can use python pass statement:

Table of Content

Using pass in Conditional Statements

In a conditional statement (like an if, elif or else block), the pass statement is used when we don’t want to execute any action for a particular condition.

Python `

x = 10

if x > 5: pass # Placeholder for future logic else: print("x is 5 or less")

`

**Explanation:

Using pass in Loops

In loops (such as for or while), pass can be used to indicate that no action is required during iterations.

Python `

for i in range(5): if i == 3: pass # Do nothing when i is 3 else: print(i)

`

**Explanation:

Using pass in Classes

In classes, pass can be used to define an empty class or as a placeholder for methods that we intend to implement later.

Python `

class EmptyClass: pass # No methods or attributes yet

class Person: def init(self, name, age): self.name = name self.age = age

def greet(self):
    pass  # Placeholder for greet method

Creating an instance of the class

p = Person("Anurag", 30)

`

**Explanation: