众所周知,if语句只有在条件等于 真(True) 的情况下才会被执行,可是往往有些 if 条件块
是在错误的情况下才需要执行的。
操作系统:window7 x64
编程IDE:Pycharm 2016.1.2
Python版本:3.6.1
编辑时间:2017年4月23日
版权所有:_ O E _ , 转载请注明出处:http://blog.csdn.net/csnd_ayo
英文原文
The operator not yields True if its argument is false, False otherwise.
中文翻译
如果变量是false,那么not
关键字会产出 True
的结果,否则相反。
示例代码
if not(False):
print(not(False))
print("end")
输出内容
True
end
结论
not
关键字的返回值是一个布尔值(bool),这个关键字会对参数进行取反操作。
即参数为真(True),则返回假(False);参数为假(False),则返回真(True)。
什么参数会被认为是False
空字符串""
, 0, None
, False
, 空列表[]
, 空字典{}
, 空元组()
都相当于False。
返回值是BOOL类型
Python 代码
print(not (False))
示例输出
True
与 C++ 的关联
not
等价于 !
他们都是取反操作,性质是完全相等的。
C++代码
#include
int main(void) {
if (!(false)) {
printf("if (!(false))");
}
return 0;
}
Python代码
if not (False):
print("if not(false):")
示例代码
print("id(not(False)): ",id(not(False)))
print("id(True): ",id(True))
输出文本
id(not(False)): 1570935408
id(True): 1570935408