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,300 points
34 views

2 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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
• 56,140 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
33 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,320 points
49 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
26 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,710 points
52 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
63 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
40 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
54 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.