“如果不是”python中的条件语句

时间:2015-12-19 23:22:25

标签: python if-statement conditional

if not start: 
   new.next = None 
   return new

“如果不是”是什么意思? 这段代码什么时候执行?

与说法相同 如果开始==无:然后做点什么?

2 个答案:

答案 0 :(得分:5)

声明是

ifnot start是表达式,notboolean operator

如果操作数(此处为not)被视为 false ,则

True会返回start。 Python将所有对象视为true,除非它们是数字零,或空容器,或None对象或布尔False值。如果not是真值,则False会返回start。请参阅文档中的Truth Value Testing section

因此,如果startNone,则确实not start为真。 start也可以是0,或者是空列表,字符串,元组字典或集合。许多自定义类型也可以指定它们等于数字0或应该被视为空:

>>> not None
True
>>> not ''
True
>>> not {}
True
>>> not []
True
>>> not 0
True

注意:因为None是一个单例(在Python进程中只有该对象的一个​​副本),所以应该始终使用isis not对其进行测试。如果严格想要测试startNone,请使用:

if start is None:

答案 1 :(得分:0)

startFalse0None,空列表[],空字典{},空时执行设置...

相关问题