Is there a difference between `continue` and `pass` in a for loop in python?

前端 未结 11 1316
孤街浪徒
孤街浪徒 2020-11-27 09:05

Is there any significant difference between the two python keywords continue and pass like in the examples

for element in some_list         


        
11条回答
  •  有刺的猬
    2020-11-27 09:57

    Yes, there is a difference. continue forces the loop to start at the next iteration while pass means "there is no code to execute here" and will continue through the remainder or the loop body.

    Run these and see the difference:

    for element in some_list:
        if not element:
            pass
        print 1 # will print after pass
    
    for element in some_list:
       if not element:
           continue
       print 1 # will not print after continue
    

提交回复
热议问题