python的for循环中的`continue`和`pass`有区别吗? [英] Is there a difference between `continue` and `pass` in a for loop in python?

查看:56
本文介绍了python的for循环中的`continue`和`pass`有区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例中的两个 python 关键字 continuepass 之间是否有显着差异

for some_list 中的元素:如果不是元素:经过

for some_list 中的元素:如果不是元素:继续

我应该知道吗?

解决方案

是的,它们做的事情完全不同.pass 什么都不做,而 continue 继续下一个循环迭代.在您的示例中,如果您在 if 之后添加另一个语句,则差异将变得明显:执行 pass 后,将执行此进一步的语句.continue 后,它不会.

<预><代码>>>>a = [0, 1, 2]>>>对于 a 中的元素:...如果不是元素:... 经过... 打印元素...012>>>对于 a 中的元素:...如果不是元素:... 继续... 打印元素...12

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

for element in some_list:
    if not element:
        pass

and

for element in some_list:
    if not element:
        continue

I should be aware of?

解决方案

Yes, they do completely different things. pass simply does nothing, while continue goes on with the next loop iteration. In your example, the difference would become apparent if you added another statement after the if: After executing pass, this further statement would be executed. After continue, it wouldn't.

>>> a = [0, 1, 2]
>>> for element in a:
...     if not element:
...         pass
...     print element
... 
0
1
2
>>> for element in a:
...     if not element:
...         continue
...     print element
... 
1
2

这篇关于python的for循环中的`continue`和`pass`有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆