• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Python Pass Keyword Explained With Examples

Python Pass Keyword Explained With Examples

Author: Aditya Raj
Last Updated: May 24, 2023

Python provides us with some unique functionalities that don’t exist in other languages. One such functionality is the pass keyword. In this article, we will understand the working of the Python pass keyword with examples. 

Table of Contents
  1. What is the pass Keyword in Python?
  2. When to Use The pass Statement in Python?
  3. The Pass Keyword as a Placeholder in Conditional Statements in Python
  4. Create an Empty Function in Python
  5. Create an Empty Class in Python
  6. Conclusion

What is the pass Keyword in Python?

The pass keyword in Python is a special keyword used to define a statement that does nothing. When you need to write a Python statement that does nothing, you can write it using the pass keyword as shown below.

print("PFB")
pass
print("PythonForBeginners")

Output:

PFB
PythonForBeginners

In the above code, we have written a pass statement between two print statements. You can observe that the print statements produce the output while the pass statement does nothing.

Functionally, you might say that the pass statement is similar to Python comments. However, their functioning is entirely different. Comments in Python are ignored by the Python interpreter. On the other hand, the pass statement is parsed and executed by the Python interpreter. 

When to Use The pass Statement in Python?

We can use the pass keyword in Python in two situations.

  • When we don’t want to do anything if a certain condition occurs. 
  • When we don’t know exactly what you need to do. There might be situations where you need to implement a function, class, or method but you haven’t decided on the implementation yet. In such cases, you can use the pass keyword as a placeholder for function implementation. This will help you define the implementation of the functions or methods and implement them later.

Let us discuss the use of the pass keyword in both of these situations.

The Pass Keyword as a Placeholder in Conditional Statements in Python

Suppose that we have multiple conditional statements and we don’t want to perform any action when a particular condition occurs. In such a case, if we include a comment for the particular condition and do not write any valid Python statements, the program will raise the IndentationError exception as shown below.

x=10
if x>100:
    #I don't know what to do. Will keep it empty.
elif x>40:
    print("Pass")
else:
    print("Fail")

Output:

File "/tmp/ipykernel_7853/2306930064.py", line 4
    elif x>40:
    ^
IndentationError: expected an indented block after 'if' statement on line 2

In the above code, we don’t want to execute any statement if the value of x is greater than 100. So, we have just put a comment there. You can observe that the program runs into IndentationError as comments are ignored by the Python interpreter and it finds the if block empty. 

To solve this issue, we can use the pass statement inside the if block. After this, the program will run smoothly without getting into any errors. You can observe this in the following example.

x=10
if x>100:
    #I don't know what to do. Will keep it empty. WIll use pass here
    pass
elif x>40:
    print("Pass")
else:
    print("Fail")

Output:

Fail

Here, we have used the pass statement inside the if block. Due to this, the code in the if block isn’t considered empty and the program works smoothly.

Create an Empty Function in Python

While implementing a program, we might not know the implementation of all the functions in our program. In such a case, if we define the function name and just put a comment there, the program will run into a SyntaxError exception. You can observe this in the following example.

def new_functions(a,b):
    #function will be implemented later

Output:

  File "/tmp/ipykernel_7853/4234004695.py", line 2
    #function will be implemented later
                                       ^
SyntaxError: incomplete input

Here, the comment inside the function is ignored. Due to this, the interpreter thinks that the function body is empty and it runs into an exception saying that there is incomplete input. To avoid this, you can use the pass statement as shown below.

def new_functions(a,b):
    #function will be implemented later
    pass

Create an Empty Class in Python

In a similar manner to functions, when we define a class without any method implementations or field declarations, the program will get into an error as shown below.

class new_class:
    #its a new class

Output:

  File "/tmp/ipykernel_7853/3282010928.py", line 2
    #its a new class
                    ^
SyntaxError: incomplete input

Again, you can use the pass statement to implement an empty class definition in python as shown below.

class new_class:
    #its a new class
    pass

In a similar manner to the class definitions, you can also create empty methods inside a Python class using the pass keyword.

Conclusion

In this article, we discussed the basics of the Python pass statement. We also discussed how to implement empty classes and functions using the pass statement in Python. To learn more about Python programming, you can read this article on pandas map vs apply method in Python. You might also like this article on how to convert YAML to TOML format in Python.

I hope you enjoyed reading this article. Stay tuned for more informative articles. 

Happy learning!

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us