From the documentation of boolean expressions in Python -
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.
The expression x or y
first evaluates x; if x
is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.
So and
/ or
expressions in Python don't necessarily return True
or False
, instead -
For and
it return the last evaluated False
value, or if all values are True
, they return the last evaluated value.
For or
it returns the last evaluated True
value, or if all values are False
, it returns the last evaluated value.
So , in your case if the condition is true , it returns the value of (1, (to, orig-1))
, otherwise it returns the value of (-1, (orig+1, to))
.
Very simple examples to show this -
>>> 1 < 5 and 2 or 4
2
>>> 6 < 5 and 2 or 4
4
Also, though not directly applicable to the condition , but in a general case, for a condition like -
cond and a or b
if cond
is a True-like value and a
has a False-like value (like 0
or empty string/list/tuple, etc), it would return b
. Example -
>>> 1 < 5 and 0 or 2
2
So, it would be better to use the equivalent if..else
ternary expression, Example -
a if cond else b