How to use the pass statement in Python

+1 vote

I am in the process of learning Python and I have reached the section about the pass statement. The guide I'm using defines it as being a Null statement that is commonly used as a placeholder.

I still don't fully understand what that means though. Can someone show me a simple/basic situation where the pass statement would be used and why it is needed?

Aug 23, 2018 in Python by bug_seeker
• 15,360 points
75 views

2 answers to this question.

0 votes

Let me give you an example, you are designing a new class with some methods that you don't want to implement, yet.

class MyClass(object):
  
def meth_a(self):
      
pass
   def meth_b(self):
       print "I'm meth_b"

If you would leave out the pass, the code wouldn't run.

You would then get an

IndentationError: expected an indented block

To summarize, the pass statement does nothing particular but can act as a placeholder, as shown before.

answered Aug 23, 2018 by Priyaj
• 57,300 points
0 votes

In Python programming, pass is a null statement. The difference between a comment and pass statement in Python is that, while the interpreter ignores a comment entirely, pass is not ignored.

example

for letter in 'python':
   if letter == 'h':
      pass
      print 'This is pass block'
   print 'Current Letter :', letter

print "Good bye!"

output:

   current letter : P 

 current letter : y 

current letter : t 

This is pass block 

current letter : h 

current letter : o 

current letter : n

 Good bye!

answered Apr 5 by anonymous

Related Questions In Python

0 votes
1 answer

How to use nested if else statement in python?

The working of nested if-else is similar ...READ MORE

answered Nov 19, 2018 in Python by Nabarupa
59 views
0 votes
1 answer

How to get the size of a string in Python?

If you are talking about the length ...READ MORE

answered Jun 4, 2018 in Python by ariaholic
• 7,340 points
115 views
0 votes
2 answers

how to use print statement in python3?

The print statement has been replaced with a print() ...READ MORE

answered Jul 16, 2018 in Python by Mrunal
• 680 points
66 views
0 votes
1 answer

How to add to the python path in Windows?

You know what has worked for me ...READ MORE

answered Jul 25, 2018 in Python by Frankie
• 9,810 points
134 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 409 views
+4 votes
6 answers
0 votes
3 answers

How to get the current time in Python

FOLLOWING WAY TO FIND CURRENT TIME IN ...READ MORE

answered Apr 8 in Python by rajesh
• 1,210 points
92 views
0 votes
2 answers

How to use threading in Python?

 Thread is the smallest unit of processing that ...READ MORE

answered Apr 6 in Python by anonymous
124 views