Skip to content
Related Articles

Related Articles

Python pass Statement

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 20 Jul, 2022
View Discussion
Improve Article
Save Article

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 ignored. 

Python pass Statement Syntax:

pass

Python pass Statement Example

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 can simply place pass where empty code is not allowed, like in loops, function definitions, class definitions, or in if statements. So using pass statement user avoids this error.

Example 1: Pass statement can be used in empty functions

Python3




def function:
  pass


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

Python3




class geekClass:
  pass


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

Python3




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


Example 4: pass statement can be used with conditional statements 

Python3




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


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

Python3




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


Output:

b
c
d

To read more on Continue and Break.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!