Python pass statement is a null operation. Means nothing will happen on its execution. The interpreter does not ignore a
So Nothing will happen when the pass is executed and the result results in no operation. It can use in condition statements where you can’t leave an empty block.
But why you have to use a pass statement in python?
pass the statement is used when you only want a function without implementation. It will use for future implementation of functions, loops, etc.
don’t write the implementation of a function but you want to implement it in the future.
Examples of Python pass statement
1. Using pass in for loop
for letter in 'Hello': if letter == 'e': pass print('This is pass block') print('Current Letter :', letter) print("Good bye!")
2. Python Empty function
def function(args): pass
addition() function has no implementation.
def addition(num1, num2): # Implementation will go here pass # Pass statement addition(2, 2)
3. Python Empty Class
class Example: pass
Q: What id the difference between python comment and pass statement?
Answer: The Main difference between a comment and a
pass statement in Python is that while the interpreter ignores a comment entirely,
pass is not ignored.
Q: How to write a function with nobody in Python?
Answer: Simply use a function name without implementing it and use pass keyword same as below code.
def myfunction: pass
Important: Python has the syntactical requirement that code blocks (after
class etc.) cannot be empty.
Pass refers to ignore….as simple as it is ….if the given condition is true and the next statement is passed it ignores that value or iteration and proceeds to the next line ….. Example
for i in range(1, 10): if i % 2 == 0: pass else: print(i)
Output: Prints all the odd numbers from 1-10
Do comment if you have any doubts and suggestions on this tutorial.
Note: This example (Project) is developed in PyCharm 2019.3 (Community Edition)
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
All Python Programs are in Python 3, so it may change its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.