×

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 Time Module

Python contains many files that can be imported into a python code and used whenever we want. One of that modules is the time module. It is a good practice...

6 minutes read.

Spark and Python for big data with pyspark github

Pyspark: Apache Spark is a computational engine that handles large data sets using parallel and batch processing. Apache Spark is an open-source, distributed computing framework and collection of libraries for real-time,...

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.

Global variables in python

In Python, considering the scope, variables are categorized into global and local variables. In this article, we will discuss these two types along with examples. Any variable holds a value in...

4 minutes read.

Python SymPy

A Python package for symbolic mathematics is called SymPy. Its goal is to develop into a fully-fledged computer algebra system (CAS) while maintaining the code as straightforward as possible to...

3 minutes read.

File Explorer using Tkinter in Python

File Explorer: Users can control folders and files on a device using an application known as a file manager or file explorer. Customers can access, edit, copy, delete, and start moving files...

3 minutes read.

Crash Course on Python by Google

There is a new, free Python programming course offered by Google on Coursera. No programming experience is necessary. There are numerous Python courses available, but when you learn that Google is...

5 minutes read.

Python Set clear() Method

Python Set clear() Method The set.clear() method removes all the elements from the set. Syntax set.clear() Parameter NA Return None Example 1 # Python program explaining # the set.clear() method # initializing the set fruits = {"watermelon", "banana", "apple"} # printing the set...

2 minutes read.

Python Linked List

Linked List Linked lists non-contiguous linear data structure which is made up of nodes and used to store value and a pointer pointing to the next node. It is linked with...

6 minutes read.

Python Matrix Operations

Python Matrix In this section of the Python tutorial, we will look at the introduction of the matrix in Python programming. We will perform many operations on the Python matrix using...

21 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.

How to Call a Function in Python

How To Call a Function in Python Functions are the well-defined and structured piece of code that is used to implement specific functionality. Calling a function in python is the best...

4 minutes read.

Kite Python

Kite in Python: The Kite is a package provided by the python programming language; it works with the help of artificial intelligence and helps us write code inside the visual studio....

3 minutes read.

Reverse a String in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

4 minutes read.

Python Program to Check Leap Year

Python Program to Check Leap Year Leap year: We all know that each year has 365 or 366 days. But whenever a year has 366 days, then the year is said...

2 minutes read.

Data Structures and Algorithms using Python | Part 2

Files: A file is a location or information stored in computer storage devices. File handling is essential when the information or the data is to be held permanently. When we try to...

20 minutes read.

Hog Descriptor Opencv Python

Python programming language: 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...

3 minutes read.

Python math.cos and math.acos function

Math.cos() function In Python, the Math module is used for performing the mathematical operations. It includes the math.cos() function that is used for obtaining the cosine value of an angle in...

3 minutes read.

Difference between Package and Module in Python

What are Python modules? A file with the “.py” suffix that includes Python or C executable code is known as a module. Multiple Python commands and expressions make compose a module....

3 minutes read.

Import Function in Python

In Python programming language, the import keyword plays an important role in the whole code because it makes one module make available in another module. Import is used to structure...

3 minutes read.