Home > Backend Development > Python Tutorial > Python pass detailed introduction and example code

Python pass detailed introduction and example code

WBOY
Release: 2016-12-05 13:27:23
Original
1346 people have browsed it

Usage of Python pass:

  1. Empty statement do nothing
  2. Guaranteed complete format
  3. Guarantee semantic integrity

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

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

Corresponding to Python, it is written like this:

if true:
  pass #do nothing
else:
  #do something
Copy after login

1 The role of the pass statement in the function

When you are writing a program, the execution statement part of the idea has not been completed. At this time, you can use the pass statement to occupy the place, or it can be used as a mark, which is the code to be completed later. For example:

def iplaypython():
  pass
Copy after login

Define a function iplaypython, but the function body has not been completed yet, and it cannot be left empty without writing content, so you can use pass instead to occupy a place.

2 The role of pass statement in loop

pass is also often used to write an empty body for a compound statement. For example, if you want an infinite loop of a while statement, which does not require any operation at each iteration, you can write like this:

while True:
  pass
Copy after login

The above is just an example. In reality, it is best not to write such code, because the execution code block is pass, which means it is empty and does nothing. At this time, python will enter an infinite loop.

Thanks for reading, I hope it can help everyone, thank you for your support of this site!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template