Python pass details and example code

  • 2020-05-17 05:50:22
  • OfStack

Usage of Python pass:

Empty statement do nothing Complete format Ensure semantic integrity

Take the if statement, for example, in c or c++/Java:


if(true)
;  //do nothing
else
{
  //do something
}

For Python, you write:


if true:
  pass #do nothing
else:
  #do something

1 function of pass statement

When you are writing a program and the execution part of the statement is not complete, you can use the pass statement as a placeholder or as a token to complete the code later. Here's what it looks like:


def iplaypython():
  pass

Define a function iplaypython, but the body part of the function is not yet complete, and you can't leave the content blank, so you can use pass instead to take a place.

2 function of pass statement in loop

pass is also used to write an empty body for a compound statement. For example, if you want to loop through an while statement indefinitely, you can write:


while True:
  pass

This is just an example. In practice, it's best not to write code like this, because if the block of code is pass, which means nothing is done, python will go into a dead loop.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: