AppDividend
Latest Code Tutorials

Python Pass Statement Tutorial With Example

0 25

Get real time updates directly on you device, subscribe now.

Python Pass Statement Tutorial With Example is today’s topic. It is used as the placeholder for future implementation of functions, loops, etc. In Python programming, the pass is the null statement. The difference between a comment and the pass statement in Python is that, while the interpreter ignores the comment entirely, the pass statement is not ignored.

Python Pass Statement Tutorial With Example

It is used when the statement is required syntactically, but you do not want that code to execute. The pass statement is the null operation; nothing happens when it runs.

The pass statement is also useful in scenarios where your code will eventually go but has not been entirely written yet. The syntax of the pass is following.

pass

Let’s see the example of the pass statement in python.

# app.py

listA = [18, 21, 19, 29, 46]
for val in listA:
    pass

Above code will not do anything, but it also does not throw any error. So that is the beauty of pass statement.

Let’s see other examples with the class and functions in Python.

def function(args):
    pass

Here, if you call the function, then you will not get any value or error, and you can do the same with a class.

class App:
    pass

The most use case of the pass statement is the scenario where you are designing a new class with some methods that you do not want to implement, yet.

Python Language has the syntactical requirement that code blocks like if, except, def, class, etc. cannot be empty. Empty code blocks are however useful in the variety of different contexts in the program.

Therefore, if nothing is supposed to happen in the code, the pass statement is needed for that block not to produce the IndentationError. Alternatively, any statement (including just a term to be evaluated, like the Ellipsis literal … or a string, most often a docstring) can be used, but the pass statement makes clear that indeed nothing is supposed to happen in that block of code, and does not need to be actually run and (at least temporarily) stored in the memory.

Finally, Python Pass Statement Tutorial With Example is over. Thanks for taking.

Leave A Reply

Your email address will not be published.

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