Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python break statement
Next article icon

Pass statement inPythonis 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 afunctionis 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 functiondeffun():pass# Placeholder, no functionality yet# Call the functionfun()

Explanation:

  • function fun() is defined but contains the pass statement, meaning it does nothing when called.
  • program continues execution without any errors and the message is printed after calling the function.

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

Using pass in Conditional Statements

In aconditional 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=10ifx>5:pass# Placeholder for future logicelse:print("x is 5 or less")

Output

Explanation:

  • When x > 5, the pass statement is executed, meaning nothing happens.
  • If x <= 5, the else block runs, printing "x is 5 or less".
  • pass allows the code to compile and run without errors, even though the if block is currently empty.

Using pass in Loops

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

Python
foriinrange(5):ifi==3:pass# Do nothing when i is 3else:print(i)

Output
0124

Explanation:

  • The loop iterates through numbers from 0 to 4.
  • When i == 3, the pass statement is executed, meaning no action is taken for that iteration.
  • For other values of i, the number is printed.

Using pass in Classes

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

Python
classEmptyClass:pass# No methods or attributes yetclassPerson:def__init__(self,name,age):self.name=nameself.age=agedefgreet(self):pass# Placeholder for greet method# Creating an instance of the classp=Person("Anurag",30)

Explanation:

  • EmptyClass is defined but has no methods or attributes. The pass statement is used to define an empty class.
  • In the Person class, the greet method is defined, but it does nothing yet (as indicated by pass).
  • This allows us to define the class and method structure without having to implement the logic immediately.

Pass Statements Python Inheritance
Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp