Pass statement in python with Examples

Python PythonBasic

In this post, we are going to understand How to use the Pass statement in python with Examples. After going through this post, we are available to use the Pass statement easily.

1. What is the purpose of pass keyword in Python


In programming languages like C#, C++, if we do not want the implementation of any function/class/if-else/loop statement at the moment but still want to create these and Leave them unimplemented then the compiler does not raise a syntactic exception. We do this because we want to implement these later, so we reserve the places for these functions or classes.

But in python interpreter does not allow unimplemented function/class/if-else/loop statement, it raises IndentationError: Excepted indented block.To avoid syntactical IndentationError Python provides a Pass statement. This is used in such situations.

2.What is pass in Python


Pass statement is null statement and placeholder for future code. If the user does not want to execute any code in loops body, classes, function Pass statement can be used and Python interpreter will skip this code and leave it unexecuted.

Syntax

pass

1. Pass in Python loops while and for


Pass with While Loop

Python Pass statement can be used with the loops, if we want the loop body unimplemented and want to implement it later in the future then the Pass statement can be used as a placeholder for future implementation. If the pass statement is not used empty loop body then it raises IndentationError: expected an indented block.

Here is an example to help you understand this with code.

number = 1

while number<5:
  pass

  number = number+1

print('Pass with while loop')

Output

Pass with while loop

Pass with for Loop

In this code example, we do not want to perform any operation. But Python does not allow an unimplemented loop body,So here we are using the Pass statement as a placeholder for the empty loop body. This is a code example with for loop and Pass statement inside it.

my_list = [3,9,6,12,15,18,21,24,29]

for item in my_list:
  
  if(item%3==0):
    pass
  else:
    print('item not divisible by 3 is :',item)

Output

item not divisible by 3 is : 29

2. Pass in Python function


In this code example, we are leaving the function body unimplemented. But empty functions are not allowed in Python. Therefore it raises IndentationError: expected an indented block. Here again, Pass comes to help us down.

Check out the example below for these codes with and without Pass statements.

def my_func():
 #first function


def printmsg():
 #this is print message function

def my_func3():
  print('my function 3 ')


#calling the my_func3
my_func3()

Output

   File "main.py", line 8
    def my_func3():
    ^
IndentationError: expected an indented block

As we have seen above example empty function body raises IndentationError: expected an indented block error. This error can be easily resolved by using the python pass statement. It would be better understood Python Pass with example.

def my_func():
 #first function
 pass 

def printmsg():
 #this is print message function
 pass

def my_func3():
  print('my function 3 ')


#calling the my_func3
my_func3()


Output

my function 3 

3. Pass in Python if-else


The pass statement can be used in the if block as a placeholder for unimplemented if-body.We are checking if the input number is divisible by 3, leaving the if block empty, then to avoid IndentationError: expected an indented block

we are making use of Pass.

num = int(input('enter the number:'))

if num%3 == 0:
    pass
else:
    print('number is not divisile to 3')

Output

enter the number:16
the number is not divisible to 3

4. Pass in Python Class


Pass statements come in handy with class implementation. Using the Pass statement, we can implement the basic structure of any Domain class until we are not fully aware of the class properties and methods.

Here we are using the Pass statement for empty class-body.

class myfirstclass:
 pass

cls = myfirstclass()

Conclusion

In this article, We have understood the pass statement in python with code example. We hope you will make use of these concepts in your code.