5

In .NET VB and C#, we can use AndAlso (&&), OrElse (||) for logical operations

How about in Python, what is the equivalent logical operators? are they only limited to 'and' and 'or'?

Updated: Below is the difference between And/Or and AndAlso/OrElse
Quoted from 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.)

Dennis
  • 3,528
  • 4
  • 28
  • 40
  • 1
    What is the logical difference between `and` and `AndAlso`? I don't know VB/C#, so there *might* be a difference, but I can't imagine what. If you need Python people to answer, you should clarify the expected behaviour. – deceze Nov 30 '17 at 11:48
  • Please check this tutorial with your answers: https://www.tutorialspoint.com/python/python_basic_operators.htm Regards, – Octavio Orozco Nov 30 '17 at 11:49
  • 1
    Poking around a bit, it sounds like VB's `AndAlso` is short circuiting while `And` is not. Python's `and` is short circuiting. Is that what you're looking for? – deceze Nov 30 '17 at 11:51
  • There are **"or"**, **"and"** and **"not"** Source : http://www.tutorialspoint.com/python/logical_operators_example.htm – ssoflashy Nov 30 '17 at 11:50

2 Answers2

8

Python already does this:

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

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


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

Returns:

foo
True

bar
foo
True

There is no need for AndAlso or OrElse in Python, it evaluates boolean conditions lazily (docs).

Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
0

In python, you can use logic gates.

You can use either, for instance, and or &.

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

IMCoins
  • 3,149
  • 1
  • 10
  • 25