In Python, the pass Statement acts as a null operator or placeholder. It is applied when a statement is required syntactically, which means that the statement will be visible in the code but will be ignored; hence, it returns nothing.
The following below is the syntax of the pass statement in Python:
Syntax:
Let us see a simple example to understand how Python's pass statement works:
Explanation:
Here, we have defined a function greeting() and used the pass statement to represent the placeholder.
Since this function is empty and returns nothing due to the pass statement, there will be no output when we call this function.
The Pass statement in Conditional statements - if, else, elif, is used when we want to leave a space or placeholder for any particular condition.
Output:
The defined number n is 5 or less than 5
Explanation:
In this example, we used the pass statement in the if-block of the if-else statement. Here, the pass statement is a placeholder indicating that a piece of code can be added in the if-block in the future.
Pass statements in Loops - for, while, are used to symbolize that no action is performed and required during iteration.
Output:
0 1 2 3 4 6 7 8 9
Explanation:
When the Pass Statement is used in the 'if' condition, it will print every number in the range of 0 to 10 except 5.
A pass statement in a Class is used to define an empty class and also as a placeholder for methods that can be used later.
Explanation:
The TpointTech class has no methods or attributes; here, the pass statement is used to describe an empty class.
In the Employees class, the first and last name methods are defined, but they produce nothing, as indicated by the pass keyword.
A pass statement is a prominent way to be used as a placeholder, which does not give any output. It can be used in conditional statements, loops, functions, and classes, which provides coders with a way to define the structure of the code without executing any functionality temporarily.
We request you to subscribe our newsletter for upcoming updates.