×

Python Pass Statement

The pass statement is a null statement. The difference between pass and comment is that comment is ignored by the interpreter, whereas pass is not.

The pass statement is typically used as a placeholder, i.e. when the user is unsure of what code to write. As a result, the user simply inserts a pass at that line. When the user does not want any code to be executed, the function pass is used. As a result, the user can simply use the pass in places where empty code is not permitted, such as loops, function definitions, class definitions, or if statements. The user avoids this error by using the pass statement.

The pass statement has no effect. It can be used when a statement is required syntactically but no action is required by the program. As an example:

>>> while True:

...     pass  # Busy-wait for keyboard interrupt (Ctrl+C)

…

This is commonly used for creating minimal classes:

>>> 

>>> class MyEmptyClass:

...     pass

…

Another use for a place pass is as a placeholder for a function or conditional body when working on new code, allowing you to think at a higher level. The pass is silently disregarded:

>>> 

>>> def initlog(*args):

...     pass   # Remember to implement this!

...

Let us consider some more examples where the pass statement can be used.

Example 1

#!/usr/bin/python

for letter in 'Python':

   if letter == 'h':

      pass

      print 'This is pass block'

   print 'Current Letter :', letter

print "Good bye!"

The output for the above code is as follows:

Current Letter : P

Current Letter : y

Current Letter : t

This is pass block

Current Letter : h

Current Letter : o

Current Letter : n

Good bye!

Example 2

Pass is a null statement in Python. The interpreter does not ignore a pass statement; however, nothing happens and the statement has no effect.

When you don't write the implementation of a function but want to implement it later, the pass statement comes in handy.

You can use the pass statement to avoid compilation errors.

Let’s use an example to demonstrate how compilation errors can be avoided by using the statement.

We are given a problem with insufficient data as follows: The students with marks more than 50 are passing the test, students with marks less than 20 have daily extra classes, while the faculty still plans about the curriculum for students with marks between 50 and 20, we have to write a code assuming to update the code in the future. Using a pass statement will be helpful as we can simply replace the line after getting the entire information. The code for the same is as follows:

marks = int(input("Please enter your marks:  "))

if(marks > 50):

    print("You have passed the examination.")

if(marks <= 50 and marks > 20):

    print("Status waiting")

    pass

if(marks <= 20):

    print("You have to attend daily extra classes!")

Let’s see how the code behaves with different values as input.

Please enter your marks:  58

You have passed the examination.

Please enter your marks:  45

Status waiting

Please enter your marks:  12

You have to attend daily extra classes!

Related Topics

Python RegEx

Python RegEx (python regular expressions) is a concept of writing and finding expressions in a pool of characters easily. A Regular expression (RegEx) is a set of characters that defines search...

10 minutes read.

Python print() function

Python print() function The print() function prints the specified message to the screen or other standard output devices. Syntax print(*objects,sep=' ',end='\n',file=sys.stdout,flush=False) Parameter objects: This function represents an object, which will be converted to a string...

1 minute read.

Face Recognition in Python

In this tutorial, we will understand what is face recognition and how it is achieved in python. Face recognition is of great utility in real-world scenarios. It is an extended step...

3 minutes read.

Unit Testing in Python

The process of testing whether a particular unit is working properly or not is called “UNIT TESTING”. A unit test will check small components in your application. The first and...

7 minutes read.

What does the if __name__ == "__main__" do in Python

In this article, you will learn about the If__name__==__main__ is a statement in python to define modules and the names of the modules. This statement plays a vital role in...

3 minutes read.

Comments in the Python Programming Language

In this tutorial, we will learn how to comment in multiple lines and paragraphs in the python programming language. Commenting a Paragraph in Python Most people think the importance of comments is...

2 minutes read.

Loan Calculator using PyQt5 in Python

In the following tutorial, we will learn how to build a Loan Calculator application using the PyQt5 library in the Python programming language. So, let's get started. Introduction to the code: The heading...

4 minutes read.

apply() function in python

Pandas: Pandas is a library in python. It is an open source package in python. Pandas in python are used for data cleaning and data analysis. Data frame mainly consists of...

3 minutes read.

How to Convert int to str in Python

How to Convert int to str in Python In the last article, we studied how we convert a variable with string datatype to integer datatype and discussed different data types available...

4 minutes read.

Converting Set to List in Python

Converting ‘set’ datatype to ‘list’ datatype is called typecasting. Typecasting in programming is a method to convert one datatype into another datatype. It may happen implicitly by the defined language, called...

3 minutes read.

Python set()

Python set() Class The set() class in Python returns a new set object, optionally with elements taken from iterable.  Syntax class set([iterable]) Parameter iterable : This parameter represents a sequence, collection or an iterator object Return This function returns a...

1 minute read.

Python str() function

Python str() function The str() function in Python converts the specified value into a string. Syntax class str(object='')           or class str(object=b'', encoding='utf-8', errors='strict') Parameter object:  This parameter represents any object to convert the given...

1 minute read.

Google Python Class

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python Functionalities of Pillow Module

This instructional exercise is about "Pillow" library, one of the significant libraries of python for picture control. Pillow library of python, is an easy to use and also open source...

14 minutes read.

Python String rfind() method

The string. rfind() method in Python returns the highest index in the string if the substring sub is found else it returns -1 if the substring is not found. Syntax string.rfind(sub [,start [,end]]) Parameter sub: This parameter...

2 minutes read.

Python List pop() method

Python List pop() method The list.pop() method removes the item at the specified position in the list, and return it. If no index is specified, this method removes and returns the last item in the list. Syntax list.pop([i]) Parameter i:...

1 minute read.

Python Time Library

We will consider various functions given by the python module library with examples.This python time module helps to work on time in python to get the current time.Before going with...

4 minutes read.

Python GUI Programming

GUI (Graphical User Interface) GUI is a graphics-based operating system that uses icons and menus to interact with the user. Python mostly works on CLI (Command Line Interface). Widgets Any user interface has...

4 minutes read.

Python String rsplit() method

Python String rsplit() method The string.rsplit() method in Python splits a string into a list, starting from the right. If the "max" parameter is not specified, this method will return the...

1 minute read.

Convert int to Float in Python using Pandas

Introduction We have already learnt about how to convert float variables to int. Now let us know how to convert int variables to float So let us create another Dataset on which...

2 minutes read.