The Python Oracle

What's the equivalent of AndAlso (&&) and OrElse (||) logical operators in Python?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: RPG Blues Looping

--

Chapters
00:00 Question
01:17 Accepted answer (Score 6)
01:47 Answer 2 (Score 0)
02:05 Thank you

--

Full question
https://stackoverflow.com/questions/4757...

Question links:
https://stackoverflow.com/a/8409488/7199...
[short-circuiting]: https://en.wikipedia.org/wiki/Short-circ...

Accepted answer links:
[docs]: https://docs.python.org/2/reference/expr...

Answer 2 links:
https://docs.python.org/2/library/stdtyp...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #python27 #logicaloperators #equivalent

#avk47



ACCEPTED ANSWER

Score 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).




ANSWER 2

Score 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