×

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 Breakpoint

Introduction In Python 3.7, a brand-new created function called breakpoint() was added. Due to the close relationship between both the executable and the code of a debugging component, debugging Python programming...

4 minutes read.

Tuple in Python

Tuples Tuples are the same as the list but as we know that lists are mutable means we can change the data from it. In the tuple we cannot change the...

6 minutes read.

Python Set intersection_update() method

Python Set intersection_update() method The set.intersection_update() method in Python removes the items that is not present in both sets. It is different from the set.intersection() method, because the intersection()method returns a new set, with only  the common elements...

2 minutes read.

What is the re.sub() function in Python

There. sub () function is used to return a string by replacing the occurrences of a specific character or pattern with a replacement string. To use this function, import the...

3 minutes read.

Simple GUI calculator using PyQt5 in Python

GUI: The user is provided with information using manipulable visual widgets that don't require command-line input. These interface components respond to the user's interactions per the pre-programmed script, assisting each user's...

4 minutes read.

Python Dictionary values() method

Python Dictionary values() method The dictionary.values() method in Python returns a view object that displays a list of all the values in the specified dictionary. Syntax dictionary.values() Parameter NA Return This method returns a view object. The...

1 minute read.

Assignment Operators in Python

The prime usage of Assignment Operators is to assign values to variables. These are taken into account to do operations on values and variables. There are some special symbols in python...

4 minutes read.

Applications of Python

Top 10 Applications of Python in 2020- 2021 Python language is famous for its general-purpose nature, which enables developers to use it in every field of development. Python can be found...

6 minutes read.

Removing the First Character from the String in Python

String The string is the collection of characters. The strings in python are “immutable”; we cannot change a string once they are created. In python, whatever data we take as input...

4 minutes read.

How to create a list in Python?

How to create a list in python Python is known for its versatility and its data structures help us to perform different kinds of operations on various datasets irrespective of their...

4 minutes read.

Static Variables in Python

What is a Static Variable? The variable that remains with a constant value throughout the program or throughout the class is known as a " Static Variable ". Static variables are...

3 minutes read.

How to change the names of Columns in Python

Introduction To play with huge amounts of data, in Python we require a tool. The tool which is available in Python is Pandas. Pandas is an open-source library. It is used...

3 minutes read.

How to change a value of a tuple in Python

Python is the language for newly evolving technologies and has become one of the most popular programming languages in the world. It is used in everything right, from simple algorithm...

3 minutes read.

chr() and ord() Functions in Python

Python language contains many built-in functions which we use for many programs to work efficiently. The chr() and ord() are a few built-in functions in Python that are used for...

4 minutes read.

Bar Plot in Python

A bar chart or bar graph displays categorical data using rectangular bars with heights or lengths proportionate to the values for the data distributed in the dataset. In a bar...

16 minutes read.

Python Dictionary update() method

Python Dictionary update() method The dictionary.update() method in Python inserts the specified items to the dictionary. Syntax dictionary.update(iterable) Parameter iterable- This parameter represents a dictionary or an iterable object with key value pairs, that will...

1 minute read.

Performing Transaction Using Python MySQL

Transaction A Transaction contains a set of SQL commands used to change the data in the dataset. If we say transaction, it means that the data in the database has changed....

3 minutes read.

How to Convert A List into String In Python?

How to Convert A List into String In Python? List is a data structure in Python that can hold values of different data types. The values are enclosed in square brackets...

3 minutes read.

Python Dictionary setdefault() method

Python Dictionary setdefault() method The dictionary.setdefault () method in Python returns the value of the item with the specified key. Syntax dictionary.setdefault(keyname, value) Parameter keyname- This parameter represents the keyname of the item you want...

1 minute read.

Python BytesIO

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.