What is pass in Python?
Question
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
It will take less than 1 minute to register for lifetime. Bonus Tip - We don't send OTP to your email id Make Sure to use your own email id for free books and giveaways
Answers ( 3 )
The pass statement is used as a placeholder for future code. It is a null operation.
When the pass statement is executed , nothing happens but you avoid getting an error
when empty code is not allowed. Empty code is not allowed in loops, function definitions,
class definitions or in if statements.
Code Example :
for a in ‘DataMonk’:
if a == ‘M’:
pass
print ‘This is pass block’
print ‘current letter : ‘ , letter
print ‘Finish’
output:
Current Letter : D
Current Letter : A
Current Letter : T
Current Letter : A
This is pass block
Current Letter : O
Current Letter : N
Current Letter : K
Finish
It is used as a null operator. If statements cannot be empty, but if you for some reason have an if statement with no content , put in the “pass” statement to avoid error in the code.
Code;
a = 33
b = 200
if b>a:
pass
Pass is used when a statement is required syntactically but you do not want any command or code to execute. It is a null operation; nothing happens when it executes. We generally use it as a placeholder.
Examples:
sequence = {‘p’, ‘a’, ‘s’, ‘s’}
for val in sequence:
pass
class Example:
pass