Python pass Function Instance Usage

  • 2021-10-11 19:04:26
  • OfStack

When it comes to placeholders, you can see from the literal meaning that they occupy 1 position. Because in the actual operation, we have a lot of code is not immediately filled in, so will use pass function to solve. Let's explain the pass function, introduce the syntax, and bring examples.

1. Description

You can use the pass statement as a placeholder, or you can use it as a tag, which is the code to be completed later.

2. Grammar


pass

3. Examples


i = 3
if i <3:
  print(" Test 1")
else:
  i += 1
  pass
  print(" Test 2",i)

#pass No matter before or after, the corresponding code will be executed, and only pass Finally, it is not output, but it will not report an error 

# It can be said that it is about to execute else I don't know what to do next. 1 You can use the pass Instead of 

Extension of knowledge points:

The Role of pass Statement in Loop

pass is also often used to write an empty body for compound statements. For example, if you want an infinite loop of while statements, you don't need any operation every iteration. You can write this:


while True:
 pass

The above is just an example. In reality, it is best not to write such code, because the execution code block is pass, that is, nothing is done, and python will enter an infinite loop.


Related articles: