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,520 points
768 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
• 58,090 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, 2019 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
964 views
0 votes
0 answers

How to use the pass statement

What would be a simple/basic situation where ...READ MORE

Apr 19, 2022 in Python by Edureka
• 13,620 points
208 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 aryya
• 7,450 points
1,087 views
0 votes
3 answers

how to use print statement in python3?

Brackets are required to print the output. >>> ...READ MORE

answered Nov 25, 2021 in Python by anonymous
1,333 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,070 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
3,491 views
0 votes
3 answers

How to get the current time in Python

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

answered Apr 8, 2019 in Python by rajesh
• 1,270 points
1,712 views
0 votes
2 answers

How to use threading in Python?

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

answered Apr 6, 2019 in Python by anonymous
1,055 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP