What is Pass statement in Python ?
Pass is a special statement in Python that does nothing. It only works as a dummy statement.
The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute. Python Pass
The pass statement is a null operation; nothing happens when it executes. The pass is also useful in places where your code will eventually go, but has not been written yet.
We can use pass statements in functions, Classes, loops….etc
num = 5
for i in range(num):
pass
pass statement in functions:
def function(args):
pass
pass statement in class:
class Demo:
pass
Let’s consider another Example:
name = "Hello"
for i in name:
if i == 'e':
pass
print('Pass executed')
print(i)
H
Pass executed
e
l
l
o
break vs continue vs pass
Here is a table that compares the break
, continue
, and pass
statements in Python:
break | continue | pass |
---|---|---|
break statement is used to exit a loop early, before the loop condition becomes false. | continue statement is used to skip the current iteration of a loop and move on to the next iteration. | pass statement is used as a placeholder for a piece of code, where no action is taken. |
break statement terminates the loop, and the program control resumes after the loop. | continue statement ends the current iteration, but the loop continues with the next value. | pass statement does nothing, it is a null statement. |
break statement is used when you want to exit a loop completely. | continue statement is used when you want to skip an iteration. | pass statement is used as a placeholder when you want to keep the structure of the code but don’t want to execute any instruction. |
Here is an example of using break
statement:
fruits = ['apple', 'banana', 'cherry']
for x in fruits:
if x == "banana":
break
print(x)
apple
Here is an example of using continue
statement:
fruits = ['apple', 'banana', 'cherry']
for x in fruits:
if x == "banana":
continue
print(x)
apple
cherry
Here is an example of using pass
statement:
for x in [1, 2, 3]:
if x == 2:
pass
else:
print(x)
1
3
It’s important to note that break
statement is used to exit a loop, continue
statement is used to skip an iteration, and pass
statement is used to do nothing but it’s required by the syntax of the code in certain situations.
- The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.