Using Python pass keyword
 

Using Python pass keyword

 

The Python pass keyword which is a statement in itself basically does nothing. It can be used as a placeholder for future code.

As empty code is not allowed in the loop’s body, Python conditional statements, functions definition, class definition, etc so by using the pass keyword you can avoid these errors.

In this article, we will discuss the usage of the pass keyword along with examples.

How to use pass keyword in Python

The syntax of using the pass keyword in Python is given below –

pass

Now see the following examples –

Example 1:

The following example shows how you can define a function without its definition in Python.

def sum_of_two():
            pass

Example 2:

You can define a loop with the empty body, look at the example below.

for i in range(0,5):
              pass

Example 3:

You can create an empty class in Python by using the pass keyword now see the example below.

class employee:
          pass

Example 4:

Similarly, you can use the pass in conditional statement –

for i in range(0,5):
             if i==2:
               pass
             else
               print(i)

Alternatives of using pass keyword in Python

Alternatively, you can use None, 0, True, or a string literal with text such as “This block does nothing” they all will perform nothing in the code. But for a better pythonic approach, these should be used in the right place in Python code.

Ok, that’s all for now. Now if you have a query then write us in the comments below.

Previous Using Python break and continue keywords
Next Introduction to Python functions
 

About author

You might also like

Python Programming 0 Comments

Python Numeric Data Types

Numeric data types are numbers stored in the memory. You can perform operations such as addition, subtraction, multiplication, division, modulo, etc on numeric data. The following data types can be

Python Programming 0 Comments

Taking Input From Users In Python

Computer programs often need to interact with users to get some kind of data from them. Later this data is processed and output is displayed on the console. For example,

Python Programming 0 Comments

Introduction To The Course On Python Programming

Python is a high-level, interpreted, general-purpose programming language. It emphasizes more on code readability so its syntax is simple and easy to use. People from different areas use it for

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply