Python pass statement | Example with loop, function & class

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 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 statement is used when you only want a function without implementation. It will use of 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


Python pass statement example

2. Python Empty function


Writing a addition() function has no implementation.

3. Python Empty Class

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.

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 pass it ignores that value or iteration and proceed to the next line ….. Example

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.