Open In App

Python pass Statement

Last Updated : 19 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

The Syntax of the pass statement

pass

What is pass statement in Python?

When the user does not know what code to write, So user simply places a pass at that line. Sometimes, the pass is used when the user doesn’t want any code to execute. So users can simply place a pass where empty code is not allowed, like in loops, function definitions, class definitions, or in if statements. So using a pass statement user avoids this error.

Why Python Needs “pass” Statement?

If we do not use pass or simply enter a comment or a blank here, we will receive an IndentationError error message.

Python3




n = 26
 
if n > 26:
    # write code your here
 
print('Geeks')


Output:

IndentationError: expected an indented block after 'if' statement

Examples of Python pass statement

Let’s see some of the examples to understand pass statements in Python more clearly.

Use of pass keyword in Function

Python Pass keyword can be used in empty functions. To read more click here

Python3




def function():
  pass


Use of pass keyword in Python Class

The pass keyword can also be used in an empty class in Python.

Python3




class geekClass:
  pass


Use of pass keyword in Python Loop 

The pass keyword can be used in Python for loop, when a user doesn’t know what to code inside the loop in Python.

Python3




n = 10
for i in range(n):
   
  # pass can be used as placeholder
  # when code is to added later
  pass


Use of pass keyword in Conditional statement

Python pass keyword can be used with conditional statements.

Python3




a = 10
b = 20
 
if(a<b):
  pass
else:
  print("b<a")


Let’s take another example in which the pass statement gets executed when the condition is true.

Python3




li =['a', 'b', 'c', 'd']
 
for i in li:
    if(i =='a'):
        pass
    else:
        print(i)


Output:

b
c
d

To read more on Python Continue and Python Break.

Python The pass Keyword in If

In the first example, the pass statement is used as a placeholder inside an if statement. If the condition in the if statement is true, no action is taken, but the program will not raise a syntax error because the pass statement is present.

In the second example, the pass statement is used inside a function definition as a placeholder for implementation. This is useful when defining a function that will be used later, but for which the implementation has not yet been written.

In the third example, the pass statement is used inside a class definition as a placeholder for implementation. This is useful when defining a class that will be used later, but for which the implementation has not yet been written.

Note that in all cases, the pass statement is followed by a colon (:) to indicate the start of a code block, but there is no code inside the block. This is what makes it a placeholder statement.

Python3




# Using pass as a placeholder inside an if statement
x = 5
if x > 10:
    pass
else:
    print("x is less than or equal to 10")
 
# Using pass in a function definition as a
# placeholder for implementation
def my_function():
    pass
 
# Using pass in a class definition as
# a placeholder for implementation
 
class MyClass:
    def __init__(self):
        pass


Output

x is less than or equal to 10


Similar Reads

How to write an empty function in Python - pass statement?
In C/C++ and Java, we can write empty function as following // An empty function in C/C++/Java void fun() { } In Python, if we write something like following in Python, it would produce compiler error. # Incorrect empty function in Python def fun(): Output : IndentationError: expected an indented block In Python, to write empty functions, we use pa
1 min read
Loops and Control Statements (continue, break and pass) in Python
Python programming language provides the following types of loops to handle looping requirements. Python While Loop Until a specified criterion is true, a block of statements will be continuously executed in a Python while loop. And the line in the program that follows the loop is run when the condition changes to false. Syntax of Python Whilewhile
4 min read
Pass list as command line argument in Python
The arguments that are given after the name of the program in the command line shell of the operating system are known as Command Line Arguments. Python provides various ways of dealing with these types of arguments. One of them is sys module. sys Module A module is a file containing Python definitions and statements. The sys module provides functi
3 min read
Python | Split and Pass list as separate parameter
With the advent of programming paradigms, there has been need to modify the way one codes. One such paradigm is OOPS. In this, we have a technique called modularity, which stands for making different modules/functions which perform independent tasks in program. In this, we need to pass more than just variable, but a list as well. Let's discuss cert
4 min read
Difference between continue and pass statements in Python
Using loops in Python automates and repeats the tasks in an efficient manner. But sometimes, there may arise a condition where you want to exit the loop completely, skip an iteration or ignore that condition. These can be done by loop control statements. Loop control statements change execution from its normal sequence. When execution leaves a scop
3 min read
break, continue and pass in Python
Using loops in Python automates and repeats the tasks in an efficient manner. But sometimes, there may arise a condition where you want to exit the loop completely, skip an iteration or ignore that condition. These can be done by loop control statements. Loop control statements change execution from their normal sequence. When execution leaves a sc
5 min read
How to pass argument to an Exception in Python?
There might arise a situation where there is a need for additional information from an exception raised by Python. Python has two types of exceptions namely, Built-In Exceptions and User-Defined Exceptions.Why use Argument in Exceptions? Using arguments for Exceptions in Python is useful for the following reasons: It can be used to gain additional
2 min read
Python - pass multiple arguments to map function
The map() function is a built-in function in Python, which applies a given function to each item of iterable (like list, tuple etc.) and returns a list of results or map object. Syntax : map( function, iterable ) Parameters : function: The function which is going to execute for each iterableiterable: A sequence or collection of iterable objects whi
3 min read
Digital Low Pass Butterworth Filter in Python
In this article, we are going to discuss how to design a Digital Low Pass Butterworth Filter using Python. The Butterworth filter is a type of signal processing filter designed to have a frequency response as flat as possible in the pass band. Let us take the below specifications to design the filter and observe the Magnitude, Phase &amp; Impulse R
4 min read
Digital High Pass Butterworth Filter in Python
In this article, we are going to discuss how to design a Digital High Pass Butterworth Filter using Python. The Butterworth filter is a type of signal processing filter designed to have a frequency response as flat as possible in the pass band. Let us take the below specifications to design the filter and observe the Magnitude, Phase &amp; Impulse
5 min read
Article Tags :
Practice Tags :