Javatpoint Logo
Javatpoint Logo

How to use Pass statement in Python?

Pre-requisite: Jump statements in python-break, continue statements

Pass statement is one of the 4 jump statements in python. To explain the functionality of this statement, imagine a scenario where you have a little time, and you are trying to understand and analyze how to write a massive complex code. You started writing the code…

You want to create an outline of the program instead of writing the whole logic, which you thought of developing further later on. So, you created functions, loops, and the other necessary outline but didn't write the whole logic inside them. You cannot leave the block spaces empty as they are not allowed in C (causes compilation errors).

We need something with which we can hold some space, and the interpreter has to be able to understand the execute amid the white space, on which we can work later on. In other and simpler terms, we need a placeholder.

This is the need and use of the pass statement

Definition:

The pass statement is a jump statement in python that holds a place inside a block of code without modifying or doing anything to the code simply, a placeholder.

It is a null statement which means it does no operation other than holding the place for the future-coming code. We use this statement to avoid compilation errors during the program's execution.

Difference between a pass statement and a comment

You may wonder that the functionalities of the pass and a comment match. A comment is just one or more lines of clarity we add that the interpreter ignores. It does not even consider the existence of a comment. So, it is just empty code even if there are comments in it, but, in the case of a pass statement, the interpreter recognizes it executes it.

Being a jump statement, the pass statement does not make any unconditional jumps like break and continue statements. It rather helps the code to go in a direction. It helps make the jump through an empty block of code.

Importance:

So, you are writing a vast code, and you want to create a function in the program that you want to work with later. If you don't write anything and leave it like that:

But, if you leave them an empty block of code like that, it raises a syntax error:


How to use Pass statement in Python

Syntax of pass statement:

Using pass statement:

This does not raise any error, and is the execution jumps from the function body to the next statement without any error or operation inside the function.

Flowchart:

How to use Pass statement in Python

Important points:

  1. A pass statement is a null statement- it doesn't do any operation on its own.
  2. It's like when you go to a place with your friend; you went earlier and you sit in a spot and to save the place for your friend until he/she comes. Now, you are the pass statement holding the place.
  3. "pass" is a keyword in the python libraries.

Another scenario:

You are writing a loop, and you have to do something about it for a particular condition, and you haven't decided on what to do yet. So, you use the pass statement inside that condition. When the condition is triggered, the pass statement allows us to handle the condition without affecting the loop. You can also use a continue statement in this situation.

Difference between a continue statement and a pass statement:

How to use Pass statement in Python

As you can see in the flowcharts, in the case of the continue statement, if the statement is executed, the execution of the program jumps back to the loop for the next iteration. In contrast, in the case of the pass statement, it doesn't make any difference when executed. It acts like a bridge that helps the program go to the next code block without falling in the middle (compilation error).

Example:

Output:

t
u
t
o
r
i
Pass executed
a
l

t
u
t
o
r
i
Continue executed
l

Understanding:

Observe the above example; the same code replaces the pass statement with the continue statement. The print statement was executed when we used the continue statement in the condition, but "a" was not printed. As soon as the interpreter came across the continue statement, it skipped everything below and went to the next iteration.

But in the case of the pass statement, "a" is printed because it doesn't skip or do anything rather than holding the place.

Example programs:

1. In a function:

2. In a class

3. Inside a loop

4. In conditional statements

5. In a try-except block

Example 1:

How to use Pass statement in Python

Output:

This program will not print anything because we checked if the numbers were even and kept a pass statement to add something to print those numbers. We don't need the odd numbers, so we used the continue statement to skip.

Now, if we add that block of code in the place of the pass statement, it prints:

2 is an even number

4 is an even number

6 is an even number

8 is an even number

We have a lot of other statements available in python that have almost the same type of functionality of the pass statement, like:

  1. Return statement
  2. Yield statement
  3. Break statement
  4. Continue statement

But, in a lot of situations, we prefer to use the pass statement because,

  1. Using a pass statement doesn't alter the flow of the code. It rather helps the execution go in the right direction preventing compilation errors. (No unnecessary jumps).
  2. Pass statement is generalized. We can use it wherever we want.

return and yield - permitted to functions

break and continue: permitted to loops, switch cases, and conditional statements.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA