Online Tutorials & Training Materials | STechies.com
Register Login
Advertisement

How to Use the Pass Statement in Python?

|| 0

How to Use the Pass Statement in Python?
Stechies

The pass statement is a null operation; means nothing will happen when you execute the function.

In python, we can use pass statement as place holder too. We use pass statement as a place holder when we have to create a function for statement and don’t want to implement the code for now but leave it to implement the code in the future.

In this case, the interpreter just simply pass the code.

Example:

# Python program to explain pass statement
string1 = "Stechies"

# Pass Sting in for loop 
for value in string1:
    print("Value: ",value)
    if value == 'e':
      pass
      print('This is pass block')
print("Value: ",value)

Output:

Value:  S
Value:  t
This is pass block
Value:  e
Value:  c
Value:  h
Value:  i
This is pass block
Value:  e
Value:  s

In the following example we have used the pass statement to simply pass the function because we did not add any function code.

Example with pass statement

def abcfunction():
pass

Example with empty function

def abcfunction():

Output:

File "pass.py", line 2

                      ^
SyntaxError: unexpected EOF while parsing

When we use the empty function python interpreter search for the function code and throws the syntax error.

Can we use comment instead of pass?

We can not use comment instead of pass because the interpreter will ignore the comment and pass statement returns null.


Advertisement
Advertisement
Advertisement