In this tutorial, we are going to learn how to have an empty block in Python using the pass
statement. Some times you have to create an empty function or leave a block of code empty. For example, if you want to leave the if
block empty, then you have to use the pass
keyword to avoid errors. Let's see the samples.
Empty blocks using pass
statement
Let's see how to create hollow blocks in the if
conditionals.
if True:
pass
else:
print('False')
The if
block in the above code is empty and the else
block will print False.
Empty function using the pass
statement
We define a normal function in python using the def
keyword.
def function_name():
But if we do not provide any definition to the function, then we will get the error, like with the above code. We have to put a statement inside the function to avoid the mistakes. See how we do it in Python.
def function_name():
pass
We don't get any errors with the above code.
Conclusion:
If you have any doubts in the tutorial, mention them in the comment section.
You may also like: