A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-pass-statement/ below:

Python pass Statement - GeeksforGeeks

Python pass Statement

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:

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:


Pass Statements Python Inheritance


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4