C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Result: Nothing happens when the pass methods are called. Pass can be used in other places in Python programs.
Python program that uses pass
# This function does nothing.
# ... It is empty.
def test(n): pass
# Pass can be on a separate line.
def test_two(n):
pass
# Call our empty functions.
test(10)
test(200)
test_two(10)
test_two(200)
# We are done.
print("DONE")
Output
DONE
Python program that uses class with pass
# An empty class.
class Box: pass
# A class with an empty method.
class Bird:
def chirp(self):
print("Bird chirped")
def fly(self):
pass
# Create empty class.
x = Box()
# Create class and call empty method.
b = Bird()
b.fly()
# Program is done.
print("Z")
Output
Z
And: With pass we can add "elif" clauses afterwards. This sometimes makes code clearer to read.
Python program that uses if, pass
# This method has a side effect.
# ... It prints a word.
def equals(name, value):
print("equals")
return name == value
name = "snake"
# Use if-statement with a method that has a side effect.
# ... Use pass to ignore contents of block.
if equals(name, "snake"):
pass
Output
equals
Python program that uses while, pass
import random
def x():
# Print and return a random int.
result = random.randint(0, 5)
print(result)
return result
# Continue calling x while its value is at least 1.
# ... Use no loop body.
while x() >= 1:
pass
print("DONE")
Output
2
3
1
3
1
4
3
4
0
DONE
Quote: Pass is a null operation—when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed.
Simple statements: python.org