"If not" python 中的条件语句
"If not" condition statement in python
if not start:
new.next = None
return new
"if not" 是什么意思?
这段代码什么时候执行?
这和说的一样吗
if start == None: 然后做点什么?
当start
为False
、0
、None
、空列表[]
、空字典{}
时执行空集...
if
是声明。 not start
是表达式,not
是 boolean operator.
not
returns True
如果操作数(start
这里)被认为是 false。 Python 认为所有对象都为真,除非它们是数字零、空容器、None
对象或布尔值 False
。 not
returns False
如果 start
是真值。请参阅文档中的 Truth Value Testing section。
所以如果 start
是 None
,那么 not start
确实是真的。 start
也可以是 0
,或者空列表、字符串、元组字典或集合。许多自定义类型还可以指定它们等于数字 0 或应被视为空:
>>> not None
True
>>> not ''
True
>>> not {}
True
>>> not []
True
>>> not 0
True
注意:因为 None
是单例(在 Python 进程中只有该对象的一个副本),您应该始终使用 is
或 is not
。如果你strictly 想要测试 tat start
是 None
,那么使用:
if start is None:
if not start:
new.next = None
return new
"if not" 是什么意思? 这段代码什么时候执行?
这和说的一样吗 if start == None: 然后做点什么?
当start
为False
、0
、None
、空列表[]
、空字典{}
时执行空集...
if
是声明。 not start
是表达式,not
是 boolean operator.
not
returns True
如果操作数(start
这里)被认为是 false。 Python 认为所有对象都为真,除非它们是数字零、空容器、None
对象或布尔值 False
。 not
returns False
如果 start
是真值。请参阅文档中的 Truth Value Testing section。
所以如果 start
是 None
,那么 not start
确实是真的。 start
也可以是 0
,或者空列表、字符串、元组字典或集合。许多自定义类型还可以指定它们等于数字 0 或应被视为空:
>>> not None
True
>>> not ''
True
>>> not {}
True
>>> not []
True
>>> not 0
True
注意:因为 None
是单例(在 Python 进程中只有该对象的一个副本),您应该始终使用 is
或 is not
。如果你strictly 想要测试 tat start
是 None
,那么使用:
if start is None: