|
|
要注意Python的逻辑运算符与C/C++逻辑运算符的不同(逻辑与、逻辑或、逻辑非)
在C/C++中:
逻辑与运算符为“&&”
逻辑或运算符为“||”
逻辑非运算符为“!”
而在Python中:
逻辑与运算符为“and”
逻辑或运算符为“or”
逻辑非运算符为“not”
示例代码如下:
- bool1 = True
- bool2 = False
- if bool1 and bool2 is False:
- print('boo1 and bool2 is False')
- if bool1 or bool2 is True:
- print('boo1 or bool2 is True')
- if not bool2:
- print('boo2 is False')
复制代码
运行结果如下:

延伸阅读:
在Python中什么样的对象布尔(bool)值为False,什么样的对象布尔(bool)值为True【可以用内置函数bool()判断对象的布尔值】 |
|