What is the difference between pass, continue and break in python?

Pass means do nothing. We usually use it because Python does not allow creating classes, functions, or if statements without code.

In the following example, if there is no code in i>3, an error will be thrown, so we use pass.

a = [1,2,3,4,5]
for i in a:
   if i > 3:
       pass
   print(i)
#=> 1
#=> 2
#=> 3
#=> 4
#=> 5

Continue will continue to the next element and stop the execution of the current element. So when i<3, print(i) will never be reached.

for i in a:
   if i < 3:
       continue
   print(i)
#=> 3
#=> 4
#=> 5

A break will interrupt the loop, and the sequence will not repeat. Therefore, elements after 3 will not be printed.

for i in a:
   if i == 3:
       break
   print(i)    
#=> 1
#=> 2

It is not easy to make, please like and encourage

Guess you like

Origin blog.csdn.net/weixin_42464956/article/details/107487213