昊虹君 发表于 2023-1-25 14:52

要注意Python的逻辑运算符与C/C++逻辑运算符的不同(逻辑与、逻辑或、逻辑非)

要注意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')
运行结果如下:
http://pic1.hhai.cc/pic1/2023/2023-01/001/42.png

延伸阅读:
在Python中什么样的对象布尔(bool)值为False,什么样的对象布尔(bool)值为True【可以用内置函数bool()判断对象的布尔值】
页: [1]
查看完整版本: 要注意Python的逻辑运算符与C/C++逻辑运算符的不同(逻辑与、逻辑或、逻辑非)