要注意Python逻辑运算符与C/C++逻辑运算符的不同(逻辑与、逻辑或、逻辑非)【用Python的if条件语句为示例】

在C/C++中:
逻辑与运算符为“&&”
逻辑或运算符为“||”
逻辑非运算符为“!”

而在Python中:
逻辑与运算符为“and”
逻辑或运算符为“or”
逻辑非运算符为“not”

C++中的示例代码就不给了,给Python的示例代码吧!

给示例代码前大家可以先延伸阅读下下面这篇博文:
在Python中什么样的对象布尔(bool)值为False,什么样的对象布尔(bool)值为True【可以用内置函数bool()判断对象的布尔值】

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')

运行结果如下:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/wenhao_ir/article/details/125584277
今日推荐