Skip to content Skip to sidebar Skip to footer

How Does The Logical `and` Operator Work With Integers?

So, I was playing with the interpreter, and typed in the following: In [95]: 1 and 2 Out[95]: 2 In [96]: 1 and 5 Out[96]: 5 In [97]: 234324 and 2 Out[97]: 2 In [98]: 234324 and

Solution 1:

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

Solution 2:

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

not x   Returns Trueif x isFalse, False otherwise
x and y Returns x if x isFalse, y otherwise
x or y  Returns y if x isFalse, x otherwise

1 is True, so it will return 2

Post a Comment for "How Does The Logical `and` Operator Work With Integers?"