Python pass Statement

The pass statement is a null statement. But the difference between pass and comment is that comment is ignored by the interpreter whereas pass is not ignroed. 

The pass statement is generally used as a placeholder i.e. when the user does not know what code to write. So user simply places pass at that line. Sometimes, pass is used when the user doesn’t want any code to execute. So user simply places pass there as empty code is not allowed in loops, function definitions, class definitions, or in if statements. So using pass statement user avoids this error.

Syntax:

pass

Example 1: Pass statement can be used in empty functions

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

def geekFunction:
  pass

chevron_right


Example 2: pass statement can also be used in empty class



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

class geekClass:
  pass

chevron_right


Example 3: pass statement can be used in for loop when user doesn’t know what to code inside the loop

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

n = 10
for i in range(n):
    
  # pass can be used as placeholder
  # when code is to added later
  pass

chevron_right


Example 4: pass statement can be used with conditional statements 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

a = 10
b = 20
  
if(a<b):
  pass
else:
  print("b<a")

chevron_right


Example 5: lets take another example in which the pass statement get executed when the condition is true 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

li =['a', 'b', 'c', 'd']
  
for i in li:
    if(i =='a'):
        pass 
    else:
        print(i)

chevron_right


Output:

b
c
d



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.