The Python Oracle

How does the logical `and` operator work with integers?

This video explains
How does the logical `and` operator work with integers?

--

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: Breezy Bay

--

Chapters
00:00 Question
01:03 Accepted answer (Score 30)
01:37 Answer 2 (Score 13)
01:58 Thank you

--

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

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

--

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

--

Tags
#python

#avk47



ACCEPTED ANSWER

Score 35


From the Python documentation:

The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.

Which is exactly what your experiment shows happening. All of your x values are true, so the y value is returned.

https://docs.python.org/3/reference/expressions.html#and




ANSWER 2

Score 13


It's for every item in Python, it's not dependent on the integer.

not x   Returns True if x is False, False otherwise
x and y Returns x if x is False, y otherwise
x or y  Returns y if x is False, x otherwise

1 is True, so it will return 2