Skip to content

Python pass statement | Example with loop, function & class

  • by

Python pass statement is a null operation. Means nothing will happen on its execution. The interpreter does not ignore a pass statement.

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?

The 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.

Syntax

pass

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!")

Output:

Python pass statement example

2. Python Empty function

def function(args):
    pass

Example:

Writing a 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 ifexceptdefclass 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)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6
Python 3.7
All Python Programs are in Python 3, so it may change its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *