pass Python

Pass in Python

Pass in Python

Introduction

In Python, a pass statement is a null statement but the difference between a comment and a pass statement is that comment is ignored while the interpretation and pass statement is not ignored.

Syntax

pass

Python pass Example

Now let us consider a case where a user is writing some code and suddenly hit a line like in loops, conditions, functions etc, where the user does not what to write. So simply, the user will place a pass and will go on, because they can not be left empty. So we use the pass to avoid these types of errors.

Example 1

def function:
  pass

In the above code, we used a pass statement in a function.

Example 2

class geekClass:
  pass

In the above code, we used a pass statement in a class.

write your code here: Coding Playground

Example 3

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

In the above code, we used a pass statement in a loop.

Example 4

x = 14
y = 50
 
if(x>y):
  pass
else:
  print("x<y")

In the above code, we used a pass statement in an if-else conditional statement.

Example 5

l =[1,2,3,4,5]
 
for i in l:
    if(i == 5):
        pass
    else:
        print(i)

In the above code, we used a pass statement in a loop that traverses in a list.

write your code here: Coding Playground

In python, when not sure what code to write, so for placeholder, we can write a pass statement there. We only need to place the pass statement there. We use the pass when we do not wish any code to get executed. In places where empty code is not allowed, there we can place a pass statement.

Some more examples

Example 6

s = {"Python", "Pass", "Statement", "Placeholder"
for v in s: 
    if v == "Pass"
        pass # leaving an empty if block using the pass keyword 
    else
        print("Not reached pass keyword: ", v)  

Output

Not reached pass keyword:  Python
Not reached pass keyword:  Placeholder
Not reached pass keyword:  Statement

Example 7

class Node:
    pass

class SLinkedList:
  def __init__(self):
      self.headval = None

ll = SLinkedList()
ll.headval = Node("Mon")
e2 = Node("Tue")
e3 = Node("Wed")

list1.headval.nextval = e2

e2.nextval = e3

Output

We left the node inside the Node class, so we can write it in future.

write your code here: Coding Playground

Example 8

class Node:
def __init__(self, value):
self.value = value
self.next = None


class Stack:
def __init__(self):
self.head = Node("head")
self.size = 0
def getSize(self):
return self.size
def isEmpty(self):
return self.size == 0
def peek(self): # Placing pass here and in the push method so we can write the code later
        pass
def push(self, value):
pass

if __name__ == "__main__":
stack = Stack()
for i in range(1, 11):
stack.push(i)
print(f"Stack: {stack}")

for _ in range(1, 6):
remove = stack.pop()
print(f"Pop: {remove}")
print(f"Stack: {stack}")

Output

In the above code, we used a pass statement inside the peek and push method, so we can use that space later to write their respective codes.