The Wayback Machine - https://web.archive.org/web/20241013191434/https://www.geeksforgeeks.org/python-pass-statement/
Open In App

Python pass Statement

Last Updated : 19 Sep, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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
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 & 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 & Impulse
5 min read
Digital Band 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 & Impulse R
5 min read
Implement Photoshop High Pass Filter (HPF) using OpenCV in Python
In this article, we will discuss how to implement photoshop High Pass Filter (HPF) image in Python OpenCV. High Pass Filter When we talk about Digital Signal Processing or Digital Image Processing Filters are the most fundamental concept. A Filter or a Digital Filter is used to sifting out the unwanted frequency response from the signals. A filter
5 min read
Pass function and arguments from node.js to Python
Prerequisites: How to run python scripts in node.js using the child_process module. In this article, we are going to learn how to pass functions and arguments from node.js to Python using child_process. Although Node.js is one of the most widely used web development frameworks, it lacks machine learning, deep learning, and artificial intelligence l
4 min read
How to pass an array to a function in Python
In this article, we will discuss how an array or list can be passed to a function as a parameter in Python. Pass an array to a function in Python So for instance, if we have thousands of values stored in an array and we want to perform the manipulation of those values in a specific function, that is when we need to pass an entire array to the speci
4 min read
Pass Arguments to the Metaclass from the Class in Python
Metaclasses in Python provide a powerful way to control the creation and behavior of classes. They act as the "class of a class" and allow you to customize class creation and behavior at a higher level. One interesting aspect of metaclasses is the ability to pass arguments from a class to its metaclass during the class definition. What is a Metacla
3 min read
How to pass optional parameters to a function in Python?
In Python, when we define functions with default values for certain parameters, it is said to have its arguments set as an option for the user. Users can either pass their values or can pretend the function to use theirs default values which are specified. In this way, the user can call the function by either passing those optional parameters or ju
5 min read
Pass by reference vs value in Python
Developers jumping into Python programming from other languages like C++ and Java are often confused by the process of passing arguments in Python. The object-centric data model and its treatment of assignment are the causes of the confusion at the fundamental level. In the article, we will be discussing the concept of how to pass a value by refere
7 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
7 min read
Pass JavaScript Variables to Python in Flask
In this tutorial, we'll look at using the Flask framework to leverage JavaScript variables in Python. In this section, we'll talk about the many approaches and strategies used to combine the two languages, which is an essential step for many online applications. Everything from the fundamentals to more complex subjects, such as how to handle enormo
7 min read
How to pass data to javascript in Django Framework ?
Django is a python framework for web development which works on jinja2 templating engine. When data is rendered along with the template after passing through views.py, that data becomes static on the html file along which it was rendered. As django is a backend framework, hence to use the power of python to use that data dynamically requests need t
3 min read
How to Pass Arguments to Tkinter Button Command?
When a user hits the button on the Tkinter Button widget, the command option is activated. In some situations, it's necessary to supply parameters to the connected command function. In this case, the procedures for both approaches are identical; the only thing that has to vary is the order in which you use them. Method 1: Pass Arguments to Tkinter
2 min read
How to Pass Additional Context into a Class Based View (Django)?
Passing context into your templates from class-based views is easy once you know what to look out for. There are two ways to do it - one involves get_context_data, the other is by modifying the extra_context variable. Let see how to use both the methods one by one. Explanation: Illustration of How to use get_context_data method and extra_context va
2 min read
How to pass arguments to shell script in crontab ?
In this article, we will discuss how to schedule shell scripts in crontab and to pass necessary parameters as well. First, let's create a simple script that we will be scheduled to run every 2 minutes. The below is a simple script that calculates the sum of all parameters passed and prints them to STDOUT along with the time the script was run. #! /
2 min read
How to Fix: if using all scalar values, you must pass an index
In this article, we discuss how to fix the error: if using all scalar values, you must pass an index in Python. Cases of this error occurrence: C/C++ Code # Importing pandas library import pandas as pd; # Creating a dictionary or the data to # be converted to the dataframe dict = {'column-1': 1,'column-2': 2, 'column-3': 3,'column-4': 4} # Converti
2 min read
Why do we pass __name__ to the Flask class?
The __name__ is a built-in special variable that evaluates the name of the current module. If the source file is executed as the main program, the interpreter sets the __name__ variable to have a value “__main__”. If this file is being imported from another module, __name__ will be set to the module’s name. index.html Let's look at a very basic boi
2 min read
Pass multiple columns in UDF in Pyspark
In this article, we are going to learn how to pass multiple columns in UDF using Pyspark in Python. Pyspark has numerous types of functions, such as string functions, sort functions, Window functions, etc. but do you know Pyspark has also one of the most essential types of functions, i.e., User Defined Function or UDF? UDF is a crucial feature of S
5 min read
How to pass a list as a command-line argument with argparse?
Argparse is a Python library used to parse command-line arguments in a user-friendly manner. It makes it easy to write user-friendly command-line interfaces, and it is widely used in Python applications. In this tutorial, we will discuss how to pass a list as a command-line argument using the Argparse library in Python. Passing a List as a Command
5 min read
How to Fix - "Runtimeerror: Package Fails to Pass a Sanity Check" For Numpy and Pandas
Encountering errors during package installations can be a frustrating experience for Python developers. One such error, "RuntimeError: Package Fails To Pass A Sanity Check," often arises due to compatibility issues or conflicts between Python versions and package versions. In this article, we'll address how to resolve this error specifically when u
2 min read
How to pass multiple arguments to function ?
A Routine is a named group of instructions performing some tasks. A routine can always be invoked as well as called multiple times as required in a given program.  When the routine stops, the execution immediately returns to the stage from which the routine was called. Such routines may be predefined in the programming language or designed or imple
5 min read
How to Pass a Dictionary to Django Models During Creation
When using Django, a Python web framework, we might need to create models with specific details. A common question is whether we can pass a dictionary to a Django model when creating it. In this article, we will explore if this can be done and what it means for our project. Pass a Dictionary to Django Models using **kwargs In Django, models are obj
2 min read
Using Else Conditional Statement With For loop in Python
Using else conditional statement with for loop in python In most of the programming languages (C/C++, Java, etc), the use of else statement has been restricted with the if conditional statements. But Python also allows us to use the else condition with for loops. The else block just after for/while is executed only when the loop is NOT terminated b
2 min read
Article Tags :
Practice Tags :