“如果不是",则为“否".python中的条件语句 [英] "If not" condition statement in python
问题描述
if not start:
new.next = None
return new
如果不是"是什么意思?该代码何时执行?
what does "if not" mean? when will this code execute?
和说的一样吗如果开始==无:那要做什么?
is it the same thing as saying if start == None: then do something?
推荐答案
如果
是语句. not start
是表达式,其中 not
是
if
is the statement. not start
is the expression, with not
being a boolean operator.
not
返回 True
.Python认为所有对象均为true,除非它们是数字零,空容器或 None
对象或布尔值 False
.如果 start
是一个真值,则 not
返回 False
.请参见 真值测试部分在文档中.
not
returns True
if the operand (start
here) is considered false. Python considers all objects to be true, unless they are numeric zero, or an empty container, or the None
object or the boolean False
value. not
returns False
if start
is a true value. See the Truth Value Testing section in the documentation.
因此,如果 start
为 None
,则实际上 not start
将为true. start
也可以为 0
,或者为空列表,字符串,元组字典或集合.许多自定义类型还可以指定它们等于数字0或应视为空:
So if start
is None
, then indeed not start
will be true. start
can also be 0
, or an empty list, string, tuple dictionary, or set. Many custom types can also specify they are equal to numeric 0 or should be considered empty:
>>> not None
True
>>> not ''
True
>>> not {}
True
>>> not []
True
>>> not 0
True
注意:因为 None
是单例(在Python进程中只有该对象的一个副本),所以应始终使用 is
或不是
.如果您严格地要测试tat start
为 None
,请使用:
Note: because None
is a singleton (there is only ever one copy of that object in a Python process), you should always test for it using is
or is not
. If you strictly want to test tat start
is None
, then use:
if start is None:
这篇关于“如果不是",则为“否".python中的条件语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!