Introduction to python pass statement:
From last few articles we are learning about looping statements in python. Until now we have covered basic looping statements i.e for loop and while loop. Also we have covered topic break and continue statements in python using which we can decide whether to exit or continue the loop execution. Now let’s proceed further in this series and learn more about pass statement in python.
Python pass statement is used as a placeholder in a program.
Pass statement in python is a null statement i.e Nothing happens when this statement is executed.
Suppose we are having a loop which is not yet ready but we are planning to write it if future. Now since the loop body is empty and if we try to execute this code, The interpreter will raise an error.
Therefore to avoid interpreter error we have to put some placeholder code within the loop body. Python pass statement can be used as a placeholder within the loop body. This placeholder statement helps us in avoiding the interpreter error with zero impact on our code execution.
Difference between python comments and python pass statement:
- Python comments are the lines within the code, which are completely ignored by the interpreter.
- However python pass statement is not ignored by the interpreter, instead pass statement is executed but nothing happens.
Examples of python pass statement:
Example 1: Print all the letter of word.
In this example, We are having a word “Hello”. Now we are going to print each letter of the word. Here we are using python pass statement within for loop. This pass statement will check if the letter is equal to “l”. As we know pass statement is not going to do anything so all the letters of word “Hello” will be printed.
Word = "Hello" for var in Word: if var == "l": pass print(var) #Result: H e l l o
- Break and continue statement in python.
- While loop in python.
- For loop in python.
- Flow control and if-elif-else statements in python