Python中AndAlso(&&)和OrElse(| |)逻辑运算符的等价物是什么?

2024-04-19 23:45:56 发布

您现在位置:Python中文网/ 问答频道 /正文

在.NET VB和C中,我们可以使用AndAlso(&;)、OrElse(| |)进行逻辑操作

在Python中,等价的逻辑运算符是什么?它们是否仅限于“”和“”?在

更新:以下是And/Or和AndAlso/OrElse之间的差异
引自https://stackoverflow.com/a/8409488/719998

Or/And will always evaluate both1 the expressions and then return a result. They are not short-circuiting evaulation.

OrElse/AndAlso are short-circuiting. The right expression is only evaluated if the outcome cannot be determined from the evaluation of the left expression alone. (That means: OrElse will only evaluate the right expression if the left expression is false, and AndAlso will only evaluate the right expression if the left expression is true.)


Tags: orandtherightonlyifis逻辑
2条回答

在python中,可以使用逻辑门。在

例如,您可以使用and&。在

https://docs.python.org/2/library/stdtypes.html#bitwise-operations-on-integer-types

Python已经这样做了:

def foo():
    print ("foo")
    return True

def bar():
    print ("bar")
    return False


print (foo() or bar())
print ("")
print (bar() or foo())

退货:

^{pr2}$

在Python中不需要AndAlso或OrElse,它延迟地计算布尔条件(docs)。在

相关问题 更多 >