Python Pass

❮ Python Keywords

Example

Create a placeholder for future code:

 
for x in [0, 1, 2]:
  pass

Definition and Usage

The pass statement is used as a placeholder for future code.

When the pass statement is executed, nothing happens, but you avoid getting an error when empty code is not allowed.

Empty code is not allowed in loops, function definitions, class definitions, or in if statements.

More Examples

Example

Using the pass keyword in a function definition:

 
def myfunction():
  pass

Example

Using the pass keyword in a class definition:

 
class Person:
  pass

Example

Using the pass keyword in an if statement:

 
a = 33
b = 200

if b > a:
  pass

❮ Python Keywords

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

 defgeekFunction:
  pass

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

 classgeekClass:
  pass

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

 n =10
fori inrange(n):
    
  # pass can be used as placeholder
  # when code is to added later
  pass

Example 4: pass statement can be used with conditional statements 

 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 

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

For more, checkout W3Schools and Geeks For Geeks.