To tell Python to do nothing, we can use the pass statement. For example, in an if-else block, if a condition holds, we can tell Python to move on to the next block of code.
if x == 2:
pass #pass tells Python to do nothing
In Python, the pass statement is very useful for controlling the flow of your program. With the pass statement, we can tell Python to do nothing.
For example, let’s say we have the following if-else block and we want to check if a variable is equal to 10. For this example, we want that if the variable is equal to 10, let’s just move on to the next block of code.
To accomplish this, we can use the pass statement and Python will do nothing if the if statement holds.
if x == 10:
pass #pass tells Python to do nothing
Doing Nothing After Else in Python
In the same way as above, we can use the Python pass statement to do nothing if we want to do nothing in an else statement.
Let’s say we are in a similar situation as above, but in this case, we want to do nothing in the else statement.
To accomplish this, we will use the pass statement in the else block.
if x == 10:
do_something()
else:
pass#pass tells Python to do nothing
Hopefully this article has been helpful for you to learn how to do nothing in Python with the pass statement.