Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python通过与继续_Python - Fatal编程技术网

Python通过与继续

Python通过与继续,python,Python,我是Python新手,无法理解以下语法 item = [0,1,2,3,4,5,6,7,8,9] for element in item: if not element: pass print(element) 这给了我所有这些元素,这是有意义的,因为通过跳过这一步到下一步 但是,如果我使用continue,我将得到以下结果 item = [0,1,2,3,4,5,6,7,8,9] for element in item: if not

我是Python新手,无法理解以下语法

item = [0,1,2,3,4,5,6,7,8,9]
for element in item:
      if not element:
          pass
      print(element)
这给了我所有这些元素,这是有意义的,因为通过跳过这一步到下一步

但是,如果我使用continue,我将得到以下结果

item = [0,1,2,3,4,5,6,7,8,9]
for element in item:
      if not element:
          continue
      print(element)
有人能告诉我为什么我得不到“0”吗?0不在列表中吗?

  • “通过”只是表示“不操作”。它没有任何作用
  • “继续”打断循环并跳到循环的下一个迭代
  • “not 0”为True,因此元素为0的“if not element”将触发continue指令,并直接跳到下一个迭代:element=1

通行证
是禁止通行证。它不起任何作用。所以当
notelement
为true时,Python什么也不做,只是继续。如果测试此处产生的差异,您也可以省略整个

continue
表示:跳过循环体的其余部分,转到下一个迭代。因此,当
notelement
为true时,Python跳过循环的其余部分(
print(element)
行),并继续下一次迭代


element
为0时,
notelement
为真;请参阅。

continue
跳过它后面的语句,而
pass
不执行类似操作。实际上
pass
什么都不做,这对于处理一些语法错误非常有用,例如:

 if(somecondition):  #no line after ":" will give you a syntax error
您可以通过以下方式进行处理:

 if(somecondition):
     pass   # Do nothing, simply jumps to next line 
演示:

这将跳过
print
语句,不打印任何内容

while(True):
    pass
    print "You will see this" 

这将继续打印
,您将看到此

继续
跳过循环体的其余部分并继续循环的下一次迭代<代码>通过
什么都不做。除了已回答的
通过/继续
难题之外,您想实现什么?可能有更好的方法来扫描列表并生成所需的输出(例如,
过滤器(无,项)
while(True):
    continue
    print "You won't see this"
while(True):
    pass
    print "You will see this"