Python提供了3个名为AND、OR、NOT的逻辑运算符。这些逻辑运算符是所有主要编程语言提供的,是计算的基础。这些逻辑运算符用于组合条件语句。在本教程中,我们将学习并检查如何将这些运算符用于不同的情况。
null
与逻辑运算符
“这个” 和 “逻辑用来检查所有给定的语句是否都是真的。如果所有给定语句都为真,“and”逻辑返回真。如果给定语句之一为False,逻辑返回False。让我们举一些例子来更好地理解“and”逻辑运算符。正如我们在下面看到的,“and”逻辑运算符可以用于多个语句,这些语句可以超过2个。
# result is Trueresult = True and True# result is Falseresult = True and False# result is Trueresult = True and True and True# result is Falseresult = True and True and True and False
或逻辑运算符
“or”逻辑用于检查多个语句,如果其中至少有一个为真,则返回Tue。如果全部为假,逻辑返回假。我们可以说or运算符非常乐观,并且通过匹配至少一个True语句来尝试返回True。与and逻辑运算符类似,or逻辑运算符可以用于两个或多个语句。
# result is True
result = True or True
# result is Trueresult = True or False
# result is Falseresult = False and False and False
# result is Trueresult = False and False and True and False
非逻辑运算符
not逻辑运算符与and、or逻辑运算符不同。not逻辑运算符用于将给定的逻辑值反转为reverse。有两个逻辑值,分别称为True和False。所以不真等于假,不假等于真。让我们举一些例子来更好地理解not逻辑运算符。
# result is Falseresult = not True# result is Trueresult = not Falseif( 18 is not 19): print("That is true 18 is not 19")
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END