Pass Statement in Python Tutorial

In this section, we will learn what the pass statement is and how to use it in Python.

What is pass in Python?

In Python, blocks cannot be empty!

For example, a function’s block, a loop’s block, etc. they should at least have one statement! Otherwise, the execution engine will return an error instead!

But sometimes we want to just have a function, or loop etc. and design its body in the future!

This is where we can use the `pass` statement in order to stop the execution engine from returning error.

The `pass` statement does nothing except signals the execution engine that the target block is not completely empty! (Basically, when using the pass statement, the engine would stop returning error when face an empty function)

Example: pass in python functions

def funcOne():
    pass

Example: pass in python for loop

list1 = ["Jack","Omid","Elon"]
for element in list1: 
    pass

Example: pass in python Class

class ClassName:
    pass

In the examples above, if we remove the `pass` statement from the body of the class or functions, Python would throw an error, because it faces an empty function or class. So the use of `pass` statement will stop Python from throwing the error.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies