Python Pass Statement Tutorial
In this article, you will learn about what is Python pass statement and you will also learn How to use pass statement in Python using an appropriate example. In the previous tutorial, we have seen Python continue statement using examples. Basically in Python, the pass statement is a null statement, which is not ignored by the interpreter.
Before going through this article. let’s understand what is exactly pass statement in python and why we need to use pass statement.
What is Python pass statement?
In Python programming pass statement is a null statement. Nothing happens when the pass statement is executed.
The difference between comment and pass statement in python is that comment is ignored entirely by interpreters but the pass is not ignored.
Why use Pass statement in Python ?
Sometimes, In our Python program like for loop, while loop and if-else condition, we don’t use write any code to be executed, Then that situation we can use pass statement so that we don’t get any error.
Syntax of pass statement:
To understand the Python pass statement we will take different examples so that you can understand in better what exactly, pass statement in Python.
Example Without pass statement:
for i in range(1,11):
If you execute the above example you will get an error. To fix this problem you can use pass statement inside for loop.
Example with pass statement:
for i in range(1,11): pass
pass statement with function:
You can use pass statement with empty python function.
def printNow(): pass
pass statement with class:
pass statement can be used with empty class.
class Name: pass
pass statement with loop:
You can use pass statement inside for loop If you don’t want to execute anything.
for i in range(1, 10): pass
pass statement with condition:
In this example pass statement will be execute when the condition return True.
a = ['a', 'e', 'i', 'o', 'u'] for i in a: if i == 'o': pass else: print(i)
a e i u
In this tutorial, You have learned what is Python pass statement and also you learned how to use pass statement in Python using various examples. Actually, the Pass statement is a very useful feature in python because many times we don’t want to execute any code inside the loop but code is mandatory according to Python, Then you can use the pass keyword inside the loop.
if you don’t use the pass statement inside the loop, You will get an error. I hope this tutorial will help you. If you like this article, please comment and share it with your friends who want to learn Python programming from scratch to advanced.