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

Python pass detailed introduction and example code

黄舟
Release: 2016-12-15 09:06:34
Original
1677 people have browsed it

Usage of Python pass:

Empty statement do nothing

Ensure complete format

Ensure complete semantics

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

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

Corresponding to Python, it should be 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 a place, or it can also be used as a mark. This is code to be completed later. For example, as follows:

def iplaypython():
  pass
Copy after login

Define a function iplaypython, but the function body part is not 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 loops

pass is also often used to write an empty body for compound statements. For example, if you want an infinite loop of while statement, no operation is required for 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. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!


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